Java Abstract Class

Abstract class in the real world

Let us take an example of graphic objects. Different graphic objects are there such as circles, rectangles, triangles, etc. They all have states defined by their positions, color, etc., and behavior defined by drawing, resizing, calculating size, etc. All these object types have common things but with different implementations. We can take advantage of common things with different implementations and put these common things in an abstract class (say GraphicObjects) then extend this class in subclasses to provide specific implementations.

Abstract class in Java

Abstract class is a mechanism of implementing 0 to 100% abstraction. A class declared with an abstract keyword is known as an abstract class. It (Abstract class) may or may not contain an abstract method. Abstract classes cannot be instantiated.

Syntax

abstract class ClassName {
   // Declare Data Members

   // Declare Abstract/Non-abstract methods
}

Abstract Method: A method with no implementation without braces and followed by a semicolon.

Syntax

abstract return_type methodName();

Java Abstract class Example

package com.w3schools;

abstract class GraphicObjects {
    abstract void showShape();
}

class Circle extends GraphicObjects {
    void showShape() {
        System.out.println("Object type is Circle.");
    }
}

class Rectangle extends GraphicObjects {
    void showShape() {
        System.out.println("Object type is Rectangle.");
    }
}

class Triangle extends GraphicObjects {
    void showShape() {
        System.out.println("Object type is Triangle.");
    }

}

public class AbstractClassTest {
    public static void main(String args[]) {
        // GraphicObjects is a superclass
        // hence it's reference can contains subclass object.
        GraphicObjects obj = new Circle();
        obj.showShape();
        
        obj = new Rectangle();
        obj.showShape();
        
        obj = new Triangle();
        obj.showShape();
    }
}

Output

Object type is Circle.
Object type is Rectangle.
Object type is Triangle.

If a class has one abstract method it must be an abstract class but vice versa is not true that is it is not necessary that an abstract class have an abstract method.

//No error
abstract class GraphicObjects {
    
    //A non-abstract method declaration
    void showShape() {
        System.out.println("Show Object Details.");
    }
}

//There will be an error because class must be abstract 
//if it have one or more abstract methods.
class GraphicObjects {
    
    //An Abstract method declaration 
    abstract void showShape();
}

If a class extends an abstract class then either it has to provide an implementation of all abstract methods or declare this class as an abstract class.

package com.w3schools;

abstract class GraphicObjects{
    abstract void showShape();
}

class Circle extends GraphicObjects{
    void showShape() {
         System.out.println("Object type is Circle.");
    }             
}

class Rectangle extends GraphicObjects{
    //There will be an error here, 
    //Because Rectangle class has to provide implementation
    //of all abstract methods of extended abstract class.
}

abstract class Triangle extends GraphicObjects{
    //There will not be any error here
    //because Triangle class is declared as an abstract class
}

public class AbstractClassTest {
    public static void main(String args[]){
           GraphicObjects obj = new Circle();
           obj.showShape();
    }    
}

Like any other Java class, An abstract class can have both static and non-static data members and methods.

package com.w3schools;

abstract class GraphicObjects {
    // non static data member
    int var1 = 500;

    // static data member
    static String str1 = "w3schools.blog";

    // abstract method declaration
    abstract void showShape();

    // non abstract, non static method
    void area(int area) {
        System.out.println("Area = " + area);
    }

    // non abstract, static method
    static void displayGraphicObjects() {
        System.out.println("Graphic objects.");
    }
}

class Circle extends GraphicObjects {
    void showShape() {
        System.out.println("Object type is Circle.");
        System.out.println("Non static variable = " + var1);
    }
}

public class AbstractClassTest {
    public static void main(String args[]) {
        GraphicObjects obj = new Circle();
        obj.showShape();
        obj.area(2500);

        GraphicObjects.displayGraphicObjects();
        System.out.println("Static variable = " + GraphicObjects.str1);
    }
}

Output

Object type is Circle.
Non static variable = 500
Area = 2500
Graphic objects.
Static variable = w3schools.blog

Why abstract class is used?

Abstract class in Java is used to implement 0 to 100% abstraction.

Note: Abstract class provides 0 to 100% abstraction because it may contain no abstract method or it may contain some of its methods as abstract methods or it may contain all its methods as abstract methods.

Can abstract classes have constructors in Java?

Yes, abstract classes have constructors in Java. However, it is not used to instantiate abstract classes. Abstract class constructors are used in constructor chaining or to initialize abstract class common variables. 

Can an abstract class be final in Java?

No, abstract classes can’t be final in Java because abstract classes are used only by extending and if they are made final they can’t extended.