Abstract Class in Java

In the world of Java programming, abstract classes play an important role in defining the structure of classes and their behavior in the hierarchy. They provide a blueprint for other teams to follow, and some methods remain undefined. This flexibility empowers developers to generate a well-organized and scalable codebase. In this section, we will explore the concept of abstract classes in Java, examining their features, advantages, and best practices.

A class that is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).

Before learning the Java abstract class, let's understand the abstraction in Java first.

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where we type the text and send the message. We do not know the internal processing about the message delivery.

Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstraction

There are two ways to achieve abstraction in Java:

Using Abstract Class (0 to 100%)

Using Interface (100%)

Abstract Class in Java

An abstract class in Java acts as a partially implemented class that itself cannot be instantiated. It exists only for subclassing purposes, and provides a template for its subcategories to follow. Abstract classes can have implementations with abstract methods. Abstract methods are declared to have no body, leaving their implementation to subclasses.

Points to Remember

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
Rules for Java Abstract class

Syntax of Abstract Classes

In Java, abstract classes are defined using the abstract keyword. Here's a basic syntax example:

In this example, Shape is an abstract class with one abstract method area() and one concrete method display(). Subclasses of Shape must implement the area() method, but they can inherit the display() method.

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of abstract method

Example of Abstract Class that has an Abstract Method

In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.

File Name: Honda4.java

Test it Now

Output:

running safely

Key Features of Abstract Classes

Abstract Methods: Abstract classes can have abstract methods, which are declared without a body. Subclasses must provide concrete implementations for these methods.

Concrete Methods: Abstract classes can also contain concrete methods with defined behavior. Subclasses inherit these methods along with their implementations.

Cannot be Instantiated: Abstract classes cannot be instantiated directly. They serve as a blueprint for other classes and must be extended to be used.

Can Have Constructors: Abstract classes can have constructors, which are invoked when a subclass object is created. These constructors are used to initialize the state of the abstract class.

When to Use Abstract Classes?

Abstract classes are beneficial in various scenarios, including:

Code Reusability: Abstract classes facilitate code reuse by allowing common methods to be implemented once and inherited by multiple subclasses.

Defining a Common Interface: Abstract classes can define a common interface for a group of related classes, ensuring consistency in their structure and behavior.

Enforcing Method Implementation: Abstract classes can enforce the implementation of certain methods by declaring them as abstract, thereby ensuring that subclasses provide necessary functionality.

Best Practices for Using Abstract Classes

To make the most out of abstract classes in Java, consider the following best practices:

Keep Abstract Classes Concise: Aim to keep abstract classes focused on a single responsibility to maintain clarity and simplicity.

Use Abstract Classes for Related Objects: Abstract classes are best suited for scenarios where a group of related objects share common characteristics and behaviors.

Prefer Interfaces for Multiple Inheritance: If a class needs to inherit from multiple sources, prefer using interfaces over abstract classes, as Java doesn't support multiple inheritance for classes.

Provide Meaningful Abstract Methods: When defining abstract methods, ensure they have clear and meaningful names that convey their purpose to subclasses.

Real Scenario of Abstract Class

In this example, Shape is the abstract class, and its implementation is provided by the Rectangle and Circle classes.

Mostly, we do not know about the implementation class (which is hidden to the end user), and an object of the implementation class is provided by the factory method.

A factory method is a method that returns the instance of the class. We will learn about the factory method later.

In this example, if we create the instance of Rectangle class, draw() method of Rectangle class will be invoked.

File Name: TestAbstraction1.java

Test it Now

Output:

drawing circle

Another Example of Abstract Class in Java

File Name: TestBank.java

Test it Now

Output:

Rate of Interest is: 7 %
Rate of Interest is: 8 %

Abstract Class Having Constructor, Data Member and Methods

An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.

File Name: TestAbstraction2.java

Test it Now

Output:

       bike is created
       running safely..
       gear changed

Rule: If there is an abstract method in a class, that class must be abstract.

Test it Now

Output:

compile time error

Rule: If we are extending an abstract class that has an abstract method, we must either provide the implementation of the method or make this class abstract.

Another Real Scenario of Abstract Class

The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.

Note: If we are beginner to java, learn interface first and skip this example.

File Name: Test5.java

Test it Now

Output:

   I am a
   I am b
   I am c
   I am d

Conclusion

Abstract classes in Java provide a powerful way to define hierarchies and promote code reuse and consistency. By providing a template for subclasses to follow, abstract classes enable developers to create a well-organized and maintainable codebase. Understanding when and how to properly use abstract classes is critical to writing robust and scalable Java applications. With the insights shared in this article, you are now ready to use dependencies with abstract classes in your Java projects.






Latest Courses