Abstraction in Java

Abstraction in Java

  • Abstraction is a process of hiding the implementation details and showing only functionality to the user.
  • Abstraction lets you focus on what the object does instead of how it does it.
  • For example, sending SMS where you type the text and send the message. You don't know the internal processing of the message delivery.

Ways to achieve Abstraction

  • There are two ways to achieve abstraction in java
  • Abstract class (0 to 100%)
  • Interface (100%)


Abstract in Java

  • A class that 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).
  • It needs to be extended and its method implemented. 

Characteristics:

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

Abstract Class Example:
abstract class A{} 

Abstract Method Example:
abstract void printStatus();

Full Example:
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(); 

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form