What is an Abstract Class in Java and How to Implement It?

Abstract 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).

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What is an Abstract Class?

what_is_an_abstract_class

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Features of Abstract Class

Java-Abstract_Class_in_Java

Template

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.

Loose Coupling

Data abstraction in Java enables loose coupling, by reducing the dependencies at an exponential level.

Code Reusability

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.

Abstraction

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.

Dynamic Resolution

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.

The Syntax for 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;

}

Examples for Abstract Classes

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.

Rules to Declare Abstract Class

Rules_to_decalre_an_Abstract_Class_in_Java-Abstract_Class

The important rules that we need to follow while using an abstract class in Java are as follows:

  • The keyword "abstract" is mandatory while declaring an abstract class in Java.
  • Abstract classes cannot be instantiated directly.
  • An abstract class must have at least one abstract method.
  • An abstract class includes final methods.
  • An abstract class may also include non-abstract methods.
  • An abstract class can consist of constructors and static methods.

Procedure to Achieve Abstraction in Java

Ways_of_data_abstraction-Abstract_Class_in_Java.

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.

Interface

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

Interface V/S Abstract Class 

interface/abstract

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

Advantages of Abstract Classes

  • Abstract class in Java is highly beneficial in writing shorter codes
  • Abstraction in Java avoids code duplication
  • Abstract classes enable code reusability
  • Changes to internal code implementation are done without affecting classes

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 Post Graduate Program In Full Stack Web Development and get certified today.

Disadvantages of Abstract Classes

  • Abstraction in Java is expensive, as sometimes you need to handle cases and situations which are not always necessary
  • Object-relational impedance mismatch, in case of RDBMS
  • Object-relational mapping occurs in case of frameworks like hibernate

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 Post Graduate Program In Full Stack Web Development which is curated by the most experienced real-time industry experts.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.