Abstract Class in Java

Let’s learn about the Abstract Class in Java. Abstraction in Java or Object oriented programming is a way to segregate implementation from interface. Abstraction in Java is achieved by using interface and abstract class in Java. Interface give 100% abstraction and abstract class give 0-100% abstraction.

In order to use interface or abstract class, we need to extend and implement an abstract method with concrete behavior.

Abstraction is a process of hiding the implementation details and showing only functionality to the user. There are two ways to achieve Abstraction in Java –

  • Abstract Class

  • Interface


Abstract Class in Java

A class declared as abstract is known as an Abstract class or If a class contains any abstract method then the class is declared as an Abstract class. you can not create the instance of an abstract class. Abstract class must be extended and it’s methods must be implemented in the immediate concrete class.

Declaration —  abstract class Credit{ }

Abstract class must be declared with the abstract keyword. Abstract class must have at least one abstract method.


Abstract Method

A method which is declared with abstract and doesn’t have implementation is called the Abstract method. An abstract method in Java doesn’t have the body, it’s just a declaration. In order to use an abstract method, you need to override that method in the immediate concrete Subclass.

Declaration —  abstract void withdrawal() ;


Example of Abstract Class

Output :

Value of i is 10
Value of j is 20
Hi, I am abstract Method


When to use Abstract Class

Abstract methods are usually declared where two or more subclasses are supposed to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract class’s abstract methods.

Example – Suppose you are creating a class called Vehicle, you know well there should be functions like start() and stop() but don’t know how that start and stop method should work, because every vehicle can have different start and stop mechanism i.e some vehicles can be started by kicking or some can be by pressing self-buttons .

So the implementation of those start() and stop() methods should be left to their concrete implementation e.g. Scooter, MotorBike, Car etc.


Points: Need to Remember about Abstract Class

  • You can’t create the instance of an abstract class using the new operator.
  • the abstract is a keyword in Java, which can be used with both class and method.
  • An abstract class can contain both abstract and concrete method.
  • An abstract method doesn’t have a body, just declaration.
  • You can’t declare the variables as abstract.
  • If a class extends an abstract class then it has to provide implementation to all its abstract method.
  • An abstract class can also extend the another Abstract class.
  • If there is any abstract method in a class, that class must be abstract.


Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply