Thursday, April 19, 2018

Abstraction [Abstract class ] in java

Abstraction is a process in any Object Oriented Programming language that is used for hiding the internal implementation details of an application and showing only the functionality to the end user. In other words, it focuses on ‘what is done’ rather that ‘how it is done’.

Example of abstraction:

When you send an email using Gmail, you just open a window, type your text and click on the send button. Once the email is sent, a message is displayed saying that ‘your message has been sent successfully’.

In this case, your only concern is whether the email is sent to the intended recipient or not (what is done), you are not concerned with how the email is sent, how your web browser connected to the server, how the server dispatched the email to the intended recipient etc. (how it is done).

Java achieves abstraction through Abstract classes and Interfaces.

This topic covers the in depth learning about Abstract classes in JAVA.

Abstract class in JAVA:

Java provides the concept of an “abstract class” to achieve abstraction.
A class that is declared using an ‘abstract’ keyword is called an ‘abstract class’.
Eg: abstract class A{}
In the above example, Class ‘A’ is declared using the ‘abstract’ keyword, hence it is an abstract class.
  • An abstract class can have both abstract and non-abstract methods.
  • A class can be ‘abstract’ even if it does not contain any abstract method. But, even if one method in a class is abstract, then the class has to be an ‘abstract’ class.
  • An abstract class cannot be instantiated, i.e. you cannot create an object of an abstract class, the methods of the abstract class can only be implemented in the child class.
  • The class which extends abstract class must implement or provide method body to all abstract method.
  • If a sub-class extends an abstract class, then it has to provide implementations for all the abstract methods in the parent abstract class. If it does not provide implementation for all the abstract methods, the sub-class should also be declared ‘abstract’.

Abstract methods in JAVA:

An abstract method, like an abstract class, is also declared using the ‘abstract’ keyword. It is a blank method which does not have a body and is required to be overridden by the sub-class to provide its implementation.

E.g.: abstract void abstractMethodExample ();

In the above example, the method ‘abstractMethodExample’ is an abstract method since it is defined with an abstract keyword and does not provide any implementation in itself.

Why do we use an Abstract Class?

An abstract class serves as a partial template to the inheriting classes. Abstract classes are used to hide implementation details and only show functionality to the user. They are used where some methods with same prototype (method declaration) is to be used by different classes. So method prototype would be same but functionality would be different depending on the requirement of the class implementing it.

Note that an abstract class can have both abstract and non-abstract methods. You can override the abstract method according to your requirement in the sub-classes.
e.g. :
abstract class University {
 void displayWelcomeMessage() {
  system.out.println("Welcome to XYZ University.")
 abstract void displayCollegeName();
Class CollegeA {
 void displayCollegeName() {
  system.out.println("Welcome to College A.")
Class CollegeB {
 void displayCollegeName() {
  system.out.println("Welcome to College B.")
class Test {
 public static void main(String args[]) {
  University u1 = new CollegeA();
  University u2 = new CollegeB();

Welcome to XYZ University.
Welcome to College A.
Welcome to College B.
In the above example, the abstract class ‘University’ has one non-abstract method ‘displayWelcomeMessage’ and one abstract method ‘displayCollegeName’.

The sub-classes ‘CollegeA’ and ‘CollegeB’ override the abstract method ‘displayCollegeName’ in their specific way to display welcome message of the respective colleges. The non-abstract method ‘displayWelcomeMessage’ displays a general welcome message of the University and does not need a specific implementation by the sub-classes.
Please refer to the ‘Example of an Abstract Class’ in the next section for better understanding.

How to use an abstract class in JAVA?

Since an abstract class cannot be instantiated, you need to write another class (a sub-class) which inherits and provides the implementation of the abstract class methods.
Example of Abstract Class

abstract class Weather {
 abstract void getWeatherReport();

class Sunny extends Weather {
 void getWeatherReport() {
  System.out.println("The weather is sunny.");

class Winter extends Weather {
 void getWeatherReport() {
  System.out.println("The weather is cold.");

class Rainy extends Weather {
 void getWeatherReport() {
  System.out.println("The weather is rainy.");

class TestAbstraction {
 public static void main(String args[]) {
  Weather w1 = new Sunny();
  Weather w2 = new Winter();
  Weather w3 = new Rainy();

The weather is sunny.
The weather is cold.
The weather is rainy.
In the above example, ‘Weather’ is an abstract class which has an abstract method named ‘getWeatherReport ()’.

Further, there are three classes: ‘Sunny’, ’Winter’ and ‘Rainy’ that inherit the abstract class ‘Weather’. These are the sub-classes of the abstract class ‘Weather’.

Each of these sub-classes have a method named ‘getWeatherReport()’ which overrides the abstract method  ‘getWeatherReport() ‘ of the parent class. All the sub-classes provide a specific implementation of the overridden method as per their own requirement.

What is the difference between an Abstract class and a Concrete class (non-abstract class) in JAVA?

Concrete Class
Abstract Class
It is declared without the ‘abstract keyword’.
E.g. : Class Animal {}
It can only be declared using the ‘abstract’ keyword.
E.g.: abstract Class Animal {}
It can be instantiated.
It cannot be instantiated.
It cannot not have an abstract method.
It may or may not have an abstract method.
It is not mandatory to inherit a concrete class to use it.
It is mandatory to inherit an abstract class to use it.
It can be declared as final.
It cannot be declared as final.

Can an abstract class be final in Java?

When a class is declared using a final keyword, it cannot be extended. A final class means it is complete in itself and does not need a sub-class to extend it and provide its implementation.

Whereas, in case of an abstract class, it is mandatory to extend the class. An abstract class is an incomplete class. It must be extended and the sub-class(es) must provide the implementation of the abstract class methods.

Making an abstract class ‘final’ will stop the class from being extended, which is the only way to use an abstract class.

These two scenarios are contradictory and thus, an abstract class cannot be final. 

A compile time error will be thrown if you try to declare an abstract class as final.

Can abstract class have static methods in Java?

A static method is associated directly with a class, not to an object. You do not require an object to call a static method, it can directly be called using the class name. Static methods cannot be overridden.

Since, an object of the class is not required to call a static method, you can declare and define static methods in an abstract class. 

Although, it is not considered to be a good practice in Object Oriented programming, you are free to define a static method inside an abstract class.
abstract class A {
 static void testStaticMethod() {
  System.out.println("A static method");
Hope you have some clearity of Abstraction and Abstract classes in java

Also read:-
Thanks for reading


Post a Comment