Lesson 9 of 26By Simplilearn
Last updated on Aug 20, 20202101Abstract Class in Java does the process of hiding the intricate code implementation details from the user and just provides the user with the necessary information. This phenomenon is called Data Abstraction in Object-Oriented Programming (Java).
We will learn the process of data abstraction in Java through the following docket.
Generally, an abstract class in Java is a template that stores the data members and methods that we use in a program. Abstraction in Java keeps the user from viewing complex code implementations and provides the user with necessary information.
We cannot instantiate the abstract class in Java directly. Instead, we can subclass the abstract class. When we use an abstract class as a subclass, the abstract class method implementation becomes available to all of its parent classes.
Now moving ahead, we will learn about all the outstanding features that the abstract class in Java has to offer.
The abstract class in Java enables the best way to execute the process of data abstraction by providing the developers with the option of hiding the code implementation. It also presents the end-user with a template that explains the methods involved.
Data abstraction in Java enables loose coupling, by reducing the dependencies at an exponential level.
Using an abstract class in the code saves time. We can call the abstract method wherever the method is necessary. Abstract class avoids the process of writing the same code again.
Data abstraction in Java helps the developers hide the code complications from the end-user by reducing the project's complete characteristics to only the necessary components.
Using the support of dynamic method resolution, developers can solve multiple problems with the help of one abstract method.
Before moving forward, let’s first understand how to declare an abstract class.
To declare an abstract class, we use the access modifier first, then the "abstract" keyword, and the class name shown below.
//Syntax:
<Access_Modifier> abstract class <Class_Name> {
//Data_Members;
//Statements;
//Methods;
}
The following programs are a few examples of the abstract class in Java.
//Example 1:
package abstraction;
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 work();
@Override
public String toString() {
return "Name=" + this.Name + "::Gender=" + this.Gender;
}
public void changeName(String newName) {
this.Name = newName;
}
public void Exam() {
// TODO Auto-generated method stub
}
public void Office() {
// TODO Auto-generated method stub
}
}
package abstraction;
public class Employee extends Person {
private int EmpId;
public Employee(String EmployeeName, String Gen, int EmployeeID) {
super(EmployeeName, Gen);
this.EmpId = EmployeeID;
}
public void Office() {
if (EmpId == 0) {
System.out.println("Employee Logged Out");
} else {
System.out.println("Employee Logged In");
}
}
public static void main(String args[]) {
Person employee = new Employee("Pavithra", "Female", 1094826);
employee.Office();
employee.changeName("Pavithra Tripathy");
System.out.println(employee.toString());
}
@Override
public void work() {
// TODO Auto-generated method stub
}
}
//Output:
Employee Logged In
Name=Pavithra Tripathy::Gender=Female
There are certain rules that one should know while declaring an abstract class. Let us explore them in detail.
The important rules that we need to follow while using an abstract class in Java are as follows:
The process of Data Abstraction in Java is possible in two different ways. The first method is obviously by using the abstract class in Java, and the other one is by using an interface.
An interface in Java is a boundary between the method and the class implementing it. An interface in Java holds the method signature in it, but never the implementation of the method. In Java, we use the interface to achieve abstraction.
//Syntax:
interface <Class_Name>{
//Method_Signatures;
}
//Example:
//Interface
package simplilearn;
public interface Area {
public void Square();
public void Circle();
public void Rectangle();
public void Triangle();
}
//Class
package simplilearn;
import java.util.Scanner;
public class shapeArea implements Area {
public void Circle() {
Scanner kb = new Scanner(System.in);
System.out.println("Enter the radius of the circle");
double r = kb.nextInt();
double areaOfCircle = 3.142 * r * r;
System.out.println("Area of the circle is" + areaOfCircle);
}
@Override
public void Square() {
// TODO Auto-generated method stub
Scanner kb2 = new Scanner(System.in);
System.out.println("Value of the side the square");
double s = kb2.nextInt();
double areaOfSquare = s * s;
System.out.println("Area of the square is" + areaOfSquare);
}
@Override
public void Rectangle() {
// TODO Auto-generated method stub
Scanner kb3 = new Scanner(System.in);
System.out.println("Enter the length of the Rectangle");
double l = kb3.nextInt();
System.out.println("Enter the breadth of the Rectangle");
double b = kb3.nextInt();
double areaOfRectangle = l * b;
System.out.println("Area of the Rectangle is" + areaOfRectangle);
}
@Override
public void Triangle() {
// TODO Auto-generated method stub
Scanner kb4 = new Scanner(System.in);
System.out.println("Enter the base of the Triangle");
double base = kb4.nextInt();
System.out.println("Enter the height of the Triangle");
double h = kb4.nextInt();
double areaOfTriangle = 0.5 * base * h;
System.out.println("Area of the Triangle is" + areaOfTriangle);
}
public static void main(String[] args) {
shapeArea geometry = new shapeArea();
geometry.Circle();
geometry.Square();
geometry.Rectangle();
geometry.Triangle();
}
}
//Output:
Enter the radius of the circle
5
Area of the circle is78.55
Enter the length of the side of the square
10
Area of the square is100.0
Enter the length of the Rectangle
25
Enter the breadth of the Rectangle
45
Area of the Rectangle is1125.0
Enter the base of the Triangle
20
Enter the height of the Triangle
25
Area of the Triangle is250.0
Though an interface and abstract class perform a similar operation of data abstraction in Java, some differences separate them. The differences between them are as follows:
Interface |
Abstract Class |
Keyword used: interface |
Keyword used: abstract |
Subclasses can implement an interface |
Subclasses have to extend abstract class |
Multiple interfaces can be implemented |
One abstract class can be extended |
Supports Multiple Inheritance |
Cannot support Multiple Inheritance |
Now that the differences between an interface and abstract class are clear, let us move forward. The next part will explore the crucial advantages and disadvantages that we must consider while using an abstract class in Java
Now, let us also learn the crucial disadvantages of using an abstract class in Java.
Do you wish to become a Java Developer? Check out the Java Certification Training Course and get certified today.
So, these were the important advantages and disadvantages of the abstract class in Java.
With this, we have arrived at the end of this "Abstract Class in Java" article. We hope you enjoyed understanding the essential concepts of abstract classes in Java.
Are you are interested in Java Programming Language and getting certified as a professional Java Developer? Then, feel free to explore our Java training and certification program which is curated by the most experienced real-time industry experts.
Got a question on "Abstract Class in Java"? Please mention them in the article's comment section, and we'll have our experts answer it for you at the earliest.
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.
Java Programming: The Complete Reference You Need
Blockchain Career Guide: A Comprehensive Playbook To Becoming A Blockchain Developer
Introducing Simplilearn’s Full Stack Java Developer Master’s Program
Java EE Tutorial: All You Need To Know About Java EE
What is Inheritance in Java and How to Implement It
Free eBook: Salesforce Developer Salary Report