What is an abstract class in Java ?

Article by: Manish Methani

Last Updated: October 27, 2021 at 8:04am IST
3 min 22 sec read

Abstract class in Java is that class that contains abstract methods as well as concrete methods. Abstract methods are the methods with no body and concrete methods are the methods which contain the body. Clear?

Before understanding more about abstract methods and abstract class first we will clear why to use abstract methods?

Abstract Methods

Assume that you have to write the displayMenu() method for applications like Notepad++, Paint. Now we know that Notepad++ has different menu buttons like Font-size, paragraph, etc. whereas Paint contains different displayMenu.

Then in this case we write declare an abstract method in the parent class that has nobody. Notepad++ will implement its functionality and Paint will implement its functionality.

//Abstract class
abstract class Display{
   /* This is an abstract method, the child class
    * must implement these methods
    */
   public abstract void displayMenu();

   //Regular method 
   public void concreteDisplayMethod(){
	System.out.println("Concrete Display Method");
   }
}

class Notepad extends Display
{
    public void displayMenu(){
     	System.out.println("Notepad++ Display Menu");
   }
}

class Paint extends Display
{
    public void displayMenu(){
     	System.out.println("Paint Display Menu");
   }
 
}

//Regular class extends abstract class
class JavaClassDemo{

   public static void main(String args[]){
   //Notepad Object
    Notepad obj = new Notepad();
    obj.displayMenu();
    obj.concreteDisplayMethod();

    
    //Paint Object
    Paint objPaint = new Paint();
    objPaint.displayMenu();
    objPaint.concreteDisplayMethod();
   }
}

Output:

Notepad++ Display Menu
Concrete Display Method
Paint Display Menu
Concrete Display Method

If you try to instantiate an object of abstract class then you will get this compilation error

Main.java:36: error: Display is abstract; cannot be instantiated
	Display obj = new Display();
	              ^
1 error

Rules to use Abstract class

1) Abstract class cannot be instantiated. An abstract class can contain both abstract methods as well as concrete methods.

2) But concrete methods cannot contain abstract methods.

3) If a class contains an abstract method then that class must be an abstract class.

4) If the class extends the Abstract class then that subclass must implement the abstract method else that class will also become an abstract class.

5) If a class contains an abstract method then the class must be declared as an abstract class otherwise you should get a compilation error. Illegal case,

public class IllegalClass
{
    public abstract void doIt();
}

Here the method is declared as abstract but class is not declared as abstract. That's why the compilation error has occurred. To resolve this issue you have to declare a class as abstract also.

You declared the method as abstract but the class is not abstract that's why you see a compilation error.

Legal Case:-

public abstract class LegalClass{

     void goodMethod() {
        // lots of real implementation code here
     }
}

If a class is abstract with no methods then it works.

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com