Introduction

When a class in java is declared with a keyword i.e. ‘abstract’, the class becomes an abstract class. It can contain non-abstract methods (methods with no body) and abstract. Before diving into the abstract classes let us have a look at the abstraction part of the java.

Abstraction

In Java, the process of only showing the functionality to the user and hiding the implementation part is called abstraction.

Another way to understand abstraction is taking the example of sending an email, you compose an email, attaching files etc then you send it. You are not aware of the processes occurring internally.

Mainly, abstraction lets the user only focus on the functionality of the object not how the object works.

What is Abstract class?

In java, the class which is declared with an abstract keyword is called the abstract keyword. It can contain non-abstract as well as abstract methods. Its methods need to be extended and then needs to be implemented. It can’t be instantiated.

Some important points about abstract class

An abstract class needs to be declared with an abstract keyword to be declared as an abstract class.

  • It can contain non-abstract and abstract methods.
  • It can’t be instantiated.
  • It can also contain constructors and static methods.
  • It can also contain final methods.

NOTE: Final Methods force the subclass so that it does not change the body of the method.

Syntax

abstract class infinite{}  

Abstract Method in Java

When a method is declared with an abstract keyword and also does not contain implementation is an abstract method.

Syntax

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

Examples of abstract class in Java

The abstract class having abstract methods

Code

abstract class Car//abstract class car
  abstract void On();  //abstract method on

class BMW extends Car//class BMW extending Car
    void On(){
        System.out.println("Welcome to BMW Experience."); //implementing the abstract method On
    }
    public static void main(String args[]){ 
        Car obj = new BMW(); //Car object 
        obj.On(); 
    } 
}  

Output

Welcome to BMW Experience.

write your code here: Coding Playground

Abstract class having constructor, data member and methods

Code

abstract class vehicle{    //creating abstract class named vehicle
  vehicle(){ //constructor of class vehicle
      System.out.println("Drive Safe.");
  } 
  abstract void on();  //abstract method named on 
  void Warn(){   // another non-abstract method
      System.out.println("Do not drink and drive.");
  } 


class BMW extends vehicle//child class wxtending parent class
    void on(){   //implementation of abstract method
        System.out.println("Do not speed up.");
    } 

 
class Test//another class to run the abstract and non abstract methods
    public static void main(String args[]){ 
        vehicle obj = new BMW(); 
        obj.on(); 
        obj.Warn(); 
    } 
}  

Output

Drive Safe.
Do not speed up.
Do not drink and drive.

write your code here: Coding Playground

Another example

Code

abstract class Hospital{    // abstract class named Hospital
    abstract String getName();    // abstract method named getName
}   
class H1 extends Hospital{    // child class extending abstract class
    String getName(){ // implementing abstract method
        return "H1";  
    }   
}   
class H2 extends Hospital{    // another child class extending parent class
    String getName(){ // implementing abstract method
    return "H2";
    }   
}   
   
class Test{    // test class to run abstract method
    public static void main(String args[]){   
        Hospital a;  // object a
        a = new H1(); 
        System.out.println("The name of Hospital is: " + a.getName());   
        a = new H2(); 
        System.out.println("The name of Hospital is: "+ a.getName());
    }
}  

Output

The name of Hospital is: H1
The name of Hospital is: H2

write your code here: Coding Playground