Java Abstraction Class & Method


What is Abstraction?

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


What is Abstract class in Java?

A class that is declared as abstract is known as abstract class.

Syntax

abstract class <class-name> { }

An abstract class is something which is incomplete and you cannot create instance of abstract class.

If you want to use it you need to make it complete or concrete by extending it.

A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended.


What is Abstract method in Java?

A method that is declare as abstract and does not have implementation is known as abstract method.

If you define abstract method than class must be abstract.

Syntax

abstract return_type method_name ();

An abstract method in Java doesn't have body, it's just a declaration. In order to use abstract method you need to override that method in Subclass.


Example of Abstract class & Abstract Method

// Abstract Class
abstract class PQR  
{
    abstract void display();  // Abstract Method
    abstract int sum();
}
class ABC extends PQR
{
    public void display()
    {
        System.out.println("Welcome to Abstract Tutroial");
    }
    public int sum()
    {
        int a=10;
        int b= 20;
        return a+b;
    }
}
public class NewMain {

    public static void main(String[] args) {
        // TODO code application logic here
        ABC obj =new ABC();
        obj.display();
        int r = obj.sum();
        System.out.println("Sum of two Number is:"+r);   
    }
}
Output
Welcome to Abstract Tutroial
Sum of two Number is:30

Points to Remember

  1. Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
  2. An abstract class must have an abstract method.
  3. Abstract classes can have Constructors, Member variables and Normal methods.
  4. Abstract classes are never instantiated.
  5. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.

When to use Abstract Methods & Abstract Class?

Abstract methods are usually declared where two or more subclasses are expected 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 methods.

Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.


Advantages of Abstraction

By using abstraction, we can separate the things that can be grouped to another type.

Frequently changing properties and methods can be grouped to a separate type so that the main type need not undergo changes. This adds strength to the OOAD principle -"Code should be open for Extension but closed for Modification".

Simplifies the representation of the domain models.


Share this article on