Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Abstract Classes in Java help the users to achieve abstraction, which is the most crucial object-oriented programming practices followed during the process of software designing. In this article, we shall discuss the terminology of Abstract Classes through the following docket.
Abstract Classes in Java act as a boundary between the implementation method and its functionality. It is used to exchange the functionality between the Concrete class members and the Abstract Class.
Abstract Classes are considered as those classes that hide the Method Implementation details from the user and show only the Method Functionality. They are declared using the keyword abstract. These methods can include Abstract and Non-Abstract methods in them.
We need Abstract Classes in Java for the following reasons:
To implement an Abstract Class in Java, we need to follow the rules as described below:
The process of Abstraction in Java can be achieved by the following two methods as mentioned below:
The Syntax for defining Abstract Classes and Abstract Methods is as follows:
abstract class Edureka{}
abstract class Method();
//Abstract Class
package Abstract; public abstract class Person { private String name; private String gender; public Person(String nm, String gen) { this.name = nm; this.gender = gen; } public abstract void Studying(); @Override public String toString() { return "Name=" + this.name + "::Gender=" + this.gender; } }
//Student Class
package Abstract; public class Student extends Person { private int StudentID; public Student(String nm, String gen, int id) { super(nm, gen); this.StudentID = id; } @Override public void Studying() { if (StudentID == 0) { System.out.println("Not Studying"); } else { System.out.println("Pursuing a Degree in Bachelor of Engineering"); } } public static void main(String args[]) { Person student = new Student("Priya", "Female", 0); Person student1 = new Student("Karan", "Male", 201021); Person student2 = new Student("Kumari", "Female", 101021); Person student3 = new Student("John", "Male", 201661); student.Studying(); student1.Studying(); student2.Studying(); student3.Studying(); System.out.println(student.toString()); System.out.println(student1.toString()); System.out.println(student2.toString()); System.out.println(student3.toString()); } }
Output:
Not Studying
Pursuing a Degree in Bachelor of Engineering
Pursuing a Degree in Bachelor of Engineering
Pursuing a Degree in Bachelor of Engineering
Name=Priya::Gender=Female
Name=Karan::Gender=Male
Name=Kumari::Gender=Female
Name=John::Gender=Male
Interface | Abstract Class |
Can have only Abstract Methods | Can have Abstract and Non-Abstract Methods |
It has only Final Variables | It includes Non-Final Variables |
It has Static and Final variables only | It has Static, Non-Static, final, Non-Final variables |
Will not implement the Abstract Class | Can implement an Interface |
Implemented using “implements” Keyword | Implemented using “extends” Keyword |
Can extend only an Interface | Can extend Java Classes and Interfaces |
Members are Public by default | Members can be Private and Protected |
//Abstract Class Example
package abstactVSinterface; abstract class Shape { String objectName = " "; Shape(String name) { this.objectName = name; } abstract public double area(); abstract public void draw(); } class Rectangle extends Shape { int length, width; Rectangle(int length, int width, String name) { super(name); this.length = length; this.width = width; } @Override public void draw() { System.out.println("Rectangle is drawn "); } @Override public double area() { return (double) (length * width); } } class Circle extends Shape { double pi = 3.14; int radius; Circle(int radius, String name) { super(name); this.radius = radius; } @Override public void draw() { System.out.println("Circle is drawn "); } @Override public double area() { return (double) ((pi * radius * radius) / 2); } } class Edureka { public static void main(String[] args) { Shape rect = new Rectangle(20, 30, "Rectangle"); System.out.println("Area of rectangle: " + rect.area()); Shape circle = new Circle(20, "Cicle"); System.out.println("Area of circle is: " + circle.area()); } }
Output:
Area of rectangle: 600.0
Area of circle is: 628.0
//Interface Example
package absVSint; interface Shape { void draw(); double area(); } class Rectangle implements Shape { int length, width; Rectangle(int length, int width) { this.length = length; this.width = width; } @Override public void draw() { System.out.println("Rectangle has been drawn "); } @Override public double area() { return (double) (length * width); } } class Circle implements Shape { double pi = 3.14; int radius; Circle(int radius) { this.radius = radius; } @Override public void draw() { System.out.println("Circle has been drawn "); } @Override public double area() { return (double) ((pi * radius * radius) / 2); } } class Edureka { public static void main(String[] args) { Shape rect = new Rectangle(20, 30); System.out.println("Area of rectangle: " + rect.area()); Shape circle = new Circle(20); System.out.println("Area of circle: " + circle.area()); } }
Output:
Area of rectangle: 600.0
Area of circle: 628.0
With this, we come to an end of this article. I hope you have understood the importance of Abstraction, Syntax, functionality, Rules of Abstraction in Java and practical examples related to them.
Now that you have understood basics of Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Mention it in the comments section of this “Abstract Classes in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co