// // //
position: fixed; top: auto !important; margin-left: 112px;

Abstract Class in Java

Abstract class in Java is that class which 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 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 abstract method in parent class which has no body. Notepad++ will implement it's own functionality and Paint will implement it's own 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 class contains abstract method then that class must be an abstract class.

4) If class extends Abstract class then that subclass must implement 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 abstract class otherwise you should get a compilation error. Illegal case,

public class IllegalClass
    public abstract void doIt();

Here method is declared as abstract but class is not declared as abstract.That's why compilation error is occured. To resolve this issue you have to declare class as abstract alos.

You declared method as abstract but 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.

Previous Next Article


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Copyright 2019. All rights reserved.