Abstraction in Java

 Abstraction in Java

 Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

  1. Abstract class 
  2. Interface

Abstract class in Java:-

A class which is declared with the abstract keyword is known as an abstract class in java. It can have abstract and non-abstract methods (method with the body).

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Points to Remember

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructor and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.

 

Abstract Method in Java:-

A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of abstract method

abstract void printStatus();     //no method body and abstract  


Example of Abstract class that has an abstract method:-

abstract class Bike
  {  
 abstract void run();  
   }  
class Honda4 extends Bike
   {  
void run(){System.out.println("running safely");
   }  
public static void main(String args[])
  {  
 Bike obj = new Honda4();  
 obj.run();  
}  
}  
 
 

Comments

Popular posts from this blog

Inheritance in Java

Type Conversions

Life cycle of a Thread and creating thread class