Abstract Classes in Java and How to Use Them

Understanding Abstract Classes in Java

An abstract class is one whose declaration includes the abstract keyword. It can contain abstract methods (declared without a body) together with concrete, non-abstract methods (methods that do have a body). A regular class, in contrast, is not allowed to hold any abstract method. Abstract classes offer an abstraction level anywhere from 1% to 100%, and that level depends entirely on how many abstract methods the class declares. When an abstract class combines abstract and non-abstract methods, its abstraction falls somewhere between 1 and 100. If it contains only abstract methods, it achieves full 100% abstraction. You can add as many abstract methods as your design needs.

Watch the Hindi video walkthrough: Abstract Class Video (Hindi)

Essential Characteristics of Abstract Classes

  • It is always declared with the abstract keyword, and the JVM treats any class marked that way as abstract.
  • If a class declares an abstract method, that class must itself be abstract; otherwise the code will not compile, as this is a core rule.
  • You cannot create an object of an abstract class — in other words, 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.
  • Once a class is declared abstract, another class must extend it and provide implementations for its abstract methods.
  • If a concrete class inherits from an abstract class, it must implement every abstract method; skipping any of them causes a compilation error.
  • If another abstract class inherits it, the developer chooses whether to implement the methods there or leave them for a later subclass.

Related reading: Constructors in Java, Static Keyword in Java, and Final Methods in Java.

Defining an Abstract Method in Java

An abstract method is part of Java's abstraction mechanism. It is a method declared without a body inside an abstract class or an interface. You declare one when the parent class only needs the method signature while child classes provide the implementation. The abstract keyword enables this, and since the method has no body, it ends with a semicolon rather than curly braces. An abstract method can exist only inside an abstract class or an interface — never inside a concrete class.

Syntax for declaring one:

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

Watch the Hindi explanation: Abstract Method Video (Hindi)

Guidelines for Declaring Abstract Methods

  • Declare the method with the abstract keyword.
  • Put the abstract keyword before the method name in the declaration.
  • The method has no body; a semicolon takes the place of the curly braces.
// This is an abstract method without implementation
public abstract void calculates();

Why Abstract Methods Matter

Suppose a programmer does not want to implement a method at declaration time and would rather leave that to a derived class. The abstract keyword makes this possible. Sometimes the programmer cannot predict how the method should behave in different scenarios, so the derived class takes on the job of implementing the abstract method.

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

Explanation: Imagine a programmer wants to print data from different schools but cannot know in advance what each school will output. The programmer therefore declares an abstract method in an abstract class, letting each derived class implement the body according to its own functionality. Here we have three classes: the abstract class Data and two concrete classes. Data holds one abstract method, showData(). When a programmer extends Data, it becomes their responsibility to implement showData() in the derived classes.

Abstract Methods vs. the final Keyword

An abstract method cannot be declared final. The two concepts contradict each other: an abstract method requires redefinition in a derived class, whereas 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 is raised. Learn more about final classes in Java.

Abstract Methods vs. the private Keyword

An abstract method cannot be private, because a private method is inaccessible outside its class. An abstract method requires the programmer to implement it in a derived class, and that implementation is only possible when the method is reachable from outside the class. Since private methods cannot be reached from outside, 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 is raised — the abstract method showData() in type Data can only use a visibility modifier of public or protected. See access modifiers in Java for more detail.

Abstract Methods vs. the static Keyword

An abstract method cannot be static, because static methods cannot be overridden. If Java allowed an abstract static method (a method without a body) and someone called it through the class name — which is possible for static methods — 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 is raised. Read more about static methods in Java.

Abstract Methods vs. 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, no object exists to hold that 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 NationalSchool();
        obj.showData();
    }
}

Output: a compile-time exception is raised.

Keep Exploring Java

Browse more Java topics to deepen your understanding:

Have questions or feedback? Join the discussion in the comments.

Abstract classes sit at the heart of Java's abstraction model: they let a parent class define a contract while leaving the details to subclasses. Pair them with interfaces, constructors and access modifiers to design cleaner, more flexible code. Review the rules above whenever you are unsure whether a method can be abstract, final, private, static or synchronized.