Abstract class in java & Use of abstract class

What Is an Abstract Class in Java?

In Java, any class declared with the abstract keyword is an abstract class. It can contain both abstract methods (declarations with no body) and concrete methods (methods that have a body). A regular class, on the other hand, is not allowed to hold an abstract method. Abstract classes offer a level of abstraction that ranges from 1% to 100%, depending on how many abstract methods they contain. When an abstract class mixes abstract and non-abstract methods, its abstraction falls between 1 and 100; when it contains only abstract methods, it achieves full 100% abstraction. You may declare as many abstract methods as your design requires.

Abstract class concept diagram in Java
Abstract class overview
Abstract class properties illustration
Key properties

Watch Hindi Video

Key Characteristics of Abstract Classes

  • An abstract class is always declared with the abstract keyword; the JVM treats any class declared this way as abstract.
  • If a class declares an abstract method, that class must itself be abstract, or compilation fails.
  • You cannot create an object of an abstract class — it cannot be instantiated.
  • It may contain both abstract and non-abstract methods.
  • It can include constructors and static methods.
  • It can also contain final methods.
  • When you declare a class abstract, some class must extend it and implement its abstract methods.
  • If a concrete class inherits an abstract class, it must implement every abstract method, or a compilation error occurs.
  • If another abstract class inherits it, the developer decides whether to implement the methods now or later.

Before going further, it helps to understand abstract methods. Learn about abstract methods, and you can also review constructors in Java, the static keyword, and final methods.

Hindi Video on Abstract Methods

Defining an Abstract Method in Java

An abstract method is a method declared without a body inside an abstract class or an interface. It is part of Java's abstraction mechanism. You declare such a method when the parent class only needs the declaration and the child classes will provide the implementation. The abstract keyword is used for this purpose. Because it has no body, it is a special kind of method. You cannot declare an abstract method inside a concrete class.

The syntax for declaring an abstract method is:

// This is an abstract method
abstract return_Type methodName();

Rules for Declaring Abstract Methods

  • Use the abstract keyword to declare the method.
  • Place the abstract keyword before the method name during declaration.
  • An abstract method has no method body; instead of curly braces, it ends with a semicolon (;).
// This is an abstract method without implementation
public abstract void calculates();

Why Use an Abstract Method?

Sometimes a programmer does not want to implement a method at declaration time and prefers to leave the implementation to a derived class. This is achieved with the abstract keyword. In other cases, the programmer may not know how the method should behave in different scenarios, so the derived class takes responsibility for implementing the abstract method.

Java Abstract Method Example

abstract class Data {
    abstract void showData();
}

class NationalSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is National School's data");
    }
}

class IndianSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is Indian School's data");
    }
}

public class MainClass {
    public static void main(String arg[]) {
        Data obj = new NationalSchool();
        obj.showData();
        Data obj1 = new IndianSchool();
        obj1.showData();
    }
}

Output:

This is National School's data
This is Indian School's data

How the Example Works

Suppose a programmer wants to print data from different schools but does not know what each school will output. The programmer declares an abstract method in an abstract class so each derived class can implement its own body according to its functionality. In this example there are three classes: the abstract class Data and two concrete classes. Data contains one abstract method, showData(). When a programmer extends Data, it becomes the programmer's responsibility to implement showData() in the derived classes.

Abstract Methods and the final Keyword

An abstract method cannot be declared final. The reason is that abstract and final are opposites: an abstract method requires redefinition in a derived class, while a final method forbids redefinition in a derived class.

abstract class Data {
    abstract final void showData();
}

class NationalSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is National School's data");
    }
}

class MainClass {
    public static void main(String arg[]) {
        Data obj = new NationalSchool();
        obj.showData();
    }
}

Output: a compile-time exception occurs.

Abstract Methods and the private Keyword

An abstract method cannot be private because a private method is not accessible outside the class. An abstract method requires the programmer to provide an implementation in the derived class, and that is only possible if the method is accessible from outside the class. Since a private method is not accessible outside the class, the private keyword cannot be combined with abstract methods.

abstract class Data {
    abstract private void showData();
}

class NationalSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is National School's data");
    }
}

class MainClass {
    public static void main(String arg[]) {
        Data obj = new NationalSchool();
        obj.showData();
    }
}

Output: a compile-time exception occurs. The abstract method showData() in type Data can only set a visibility modifier, one of public or protected.

Abstract Methods and the static Keyword

An abstract method cannot be static because a static method cannot be overridden. If Java allowed an abstract static method (a method without a body) and someone called it using the class name, since static methods are accessible directly by class name, the call would target an incomplete method.

abstract class Data {
    static abstract void showData();
}

class NationalSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is National School's data");
    }
}

class MainClass {
    public static void main(String arg[]) {
        Data obj = new NationalSchool();
        obj.showData();
    }
}

Output: a compile-time exception occurs.

Abstract Methods and the synchronized Keyword

An abstract method cannot be synchronized because a thread acquires a lock on an object when it enters the method. A thread entering a synchronized method must obtain the lock of the object (or class) in which the method is defined. Since an abstract class cannot be instantiated, there is no object to hold the lock.

abstract class Data {
    Abstract synchronized void showData();
}

class NationalSchool extends Data {
    @Override
    void showData() {
        System.out.println("This is National School's data");
    }
}

class MainClass {
    public static void main(String arg[]) {
        Data obj = new Nation

Keep Learning Java

Explore related Java topics to strengthen your understanding:

Join the Discussion

Read what other learners are saying or add your own thoughts:

Leave a Reply

Stay Connected With Javagoal

Javagoal learning resource
Learn Java with Javagoal
Javagoal tutorial illustration
Tutorial illustration
Community member avatar
Community member
Community member avatar
Community member
Community member avatar
Community member
YouTube logo
YouTube
Instagram logo
Instagram

Chat Now Facebook

Abstract classes sit at the heart of Java abstraction: they let you declare a shared contract while leaving the details to subclasses. Use them when several related classes should share common code but each must supply its own version of certain methods. Pair this guide with the linked topics on abstract methods, constructors, and access modifiers to see how the pieces fit together.