Abstraction in Java

⇒ Definition :-

Abstraction is the process of hiding the implementation or details and showing the main services or the interfaces to the user. Basically Abstraction deals with exposing the interfaces to the user and hiding their implementations.

Real life Example of Abstraction: As in the car case,relevant parts like stearin,gear,horn,accelerator,breaks etc are shown to driver because they are necessary for driving. But driver need not understand the internal functioning of engine, gear, break etc. Showing the interface to the user and hiding the implementation is called as the Abstraction.

In java, abstraction is achieved by two ways:

  1. Abstract class
  2. Interface

By using interfaces we can achieve 100% Abstraction and by using Abstract class it is in between 0 to 100%.

Important terms about Abstraction

Data abstraction in oops java : An abstract class is a class that is declared with abstract keyword.

  1. An abstract class not compulsion that it can have only abstract method some of them can be concrete.
  2. Any class that having abstract method then that class must be declared with abstract keyword.
  3. Abstracted class can’t be directly instantiated using new keyword. That means we cant create the object of Abstract class.
  4. If any regular class extends an abstract class, then class must have to implement all the abstract methods of the abstract parent class

Abstract method: An abstract method is a method which is declared without an implementation.

1. A method defined abstract need to be redefine in the subclass,thus to making overriding compulsory

2. It is necessary that an abstract must have abstract class but its inverse is not right.

java program to implement abstract class

abstract class Vehicle

{

abstract void start();

}

class Car extends Vehicle

{

voidstart()

{

System.out.println("car starts with key");

}

}

class Scooter extends Vehicle

{

void start()

{

System.out.println("scooter starts with kick");

}

public static void main(String[] args)

{

Car c=new Car();

c.start();                 

Scooter s=new Scooter();

s.start();

}

}

Output:-

car starts with key

scooter starts with kick

Leave a Comment