What is an abstract class in Java ?

3 min 22 sec read Basic

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();

    //Paint Object
    Paint objPaint = new Paint();


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.

How was the tutorial? Nice. Right?

Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python