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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>