print
Advertisment
Advertisment

Abstraction in java

Abstraction is a process in which the implementation is hidden and only the functionality is shown to the user. In other words, "It only shows the necessary information to the user and the background details (internal process) are hidden.
For example:- We talk on the phone, type the message. It shows all the functionality but we do not know about its internal working, how the message is sent. And how does the call happen?

How many types of abstractions can be obtained in java

There are two ways to achieve abstraction in java

  1. Abstract class
  2. Interface

Abstraction Example in java

abstract class Car{
    abstract void start();
      }
class Audi extends Car{
    void start(){    
      System.out.println("car is started");
    }
    public static void main(String[] args) {
      Audi audi = new Audi();
      audi.start();
  }
}

Output :

car is started

Abstraction Example in java

interface Vehicle{
    public void start();
}
class Car implements Vehicle{
  public void start(){
     System.out.println("Car is started");
   }
  public static void main(String[] args) {
     Car car = new Car();
     car.start();
   }
 }
 

Output :

Car is started
Advertisment

Abstract class in java

The class which is declared with the abstract keyword is called Abstract Class. It can have abstract and non-abstract methods. A normal class does not have abstract methods.

main points of abstract class in java

  • An abstract class is always declared with abstract keywords.
  • It cannot be instantiated, that is, it cannot be used to create objects.
  • It can also have abstract and non-abstract methods.
  • To use abstract class, we have to inherit it from another class and provide implementation to abstract methods.
  • Abstract methods do not have a body.
  • The constructor cannot be made abstract.

Abstract class Example in java

abstract class A{
  public void method(){
    System.out.println("parent method");  //non-abstract mehtod
     } 
   }
class Abstract_class extends A{
  public void run(){    
    System.out.println("child method");
   }
public static void main(String[] args) {
    Abstract_class obj = new Abstract_class();
    obj.method();
  obj.run();
  }
  }
 

Output :

 
  
  parent method
  child method
  

Abstract class Example in java

abstract class A{
  public void method(){
  System.out.println("parent method");  //non-abstract mehtod
     } 
  abstract void run();   //abstract method
    public A(){System.out.println("parent constructor");}
     }
class Example extends A{
  public void run(){    
      System.out.println("abstract run method");
   }
  public static void main(String[] args) {
   Example obj = new Example();
   obj.method();
   obj.run();
     }
   }
 

Output :

 
  
  parent method
  parent constructor
  abstract run method
  
Advertisment

The abstract method in java

The method which is declared by the abstract keyword is called the abstract method. This method does not have an implementation, that is, it does not have a body.

  • In this, the abstract keyword is written in front of the method name.
  • An abstract method does not have a body.
  • Instead of curly braces, put a semicolon at the end of this method; It seems.

Abstract method Example in java

abstract class Aduri{ 
  public abstract void add();
     }
class Example extends Aduri{
  public void add(){    
      System.out.println("23 Number");
   }
public static void main(String[] args) {
     Example obj = new Example();
     obj.add();
   }
   }
 

Output :

 
  
 23 Number
  
Advertisment
Advertisment
arrow_upward