Abstraction in Java


Abstraction is a process to hiding the implementation details and only showing the functionality to the user.Abstraction lets you focus on what the object does instead of how it does it. According to this concept user can fetch the data from the class which is relevant to their application without knowing how they will operate.

for example when we drive the car then only we know that how we drive the car .There is no need to know to how the engine of the car is working.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

  1. Abstract class
  2. Interface

Abstract class in Java

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.This is explain in the example below.

abstract class car

{

abstract void run();

}

class maruti extends car

{

void run()

{

System.out.println(“running safely..”);

}

class Tata

{

public static void main(String args[])

{

car obj = new maruti();

obj.run();

}

}

 

Comments are closed.