Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

In Java, abstraction is achieved using Abstract classes and interfaces.

Abstract Class

A class which contains the abstract keyword in its declaration is known as abstract class.

1.  Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )

2.    But, if a class has at least one abstract method, then the class must be declared abstract.

3.    If a class is declared abstract, it cannot be instantiated.

4.  To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.

5.    If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

Example -

abstract class A{

void display(){

System.out.println("display");

}

}

class B extends A{

public static void main(String []arr){

B obj=new B();

obj.display();

}

}

Output -

        display


Abstract Methods

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as an abstract.

1.   abstract keyword is used to declare the method as abstract.

2.  You have to place the abstract keyword before the method name in the method declaration.

3.   An abstract method contains a method signature, but no method body.

4.  Instead of curly braces, an abstract method will have a semoi colon (;) at the end.

Abstract Class Override

Example - 

abstract class A{

abstract void display();

void show(){

System.out.println("show");

}

}

class B extends A{

public static void main(String []arr){

B obj=new B();

obj.display();

obj.show();

}

void display(){

System.out.println("display");

}

}

Output - 

        show

        display