Abstract Methods and Classes in Java help you define common behavior for related classes while leaving selected method implementations to subclasses. In this Java Tutorial, we shall learn how abstract methods and abstract classes implement Abstraction in Java, when to use them, and what rules the Java compiler expects.

Abstraction is one of the main object-oriented programming ideas in Java, along with encapsulation, inheritance, and polymorphism. An abstract class is useful when multiple subclasses share state or reusable methods, but each subclass must still provide its own implementation for one or more operations.

Abstract Methods and Classes in Java: Meaning and Purpose

An abstract method declares what a subclass must do, but it does not say how the subclass must do it. An abstract class is a class declared with the abstract keyword. It can contain abstract methods, normal methods, fields, constructors, and static members.

The main purpose of an abstract method is to enforce a common method contract on subclasses. For example, every vehicle can accelerate and brake, but a car, bike, and truck may implement those actions differently.

Java Abstract Method Syntax and Rules

An abstract method has only declaration part but no implementation or definition is provided.

An example of declaring an abstract method is as following.

</>
Copy
public abstract void printMessage();

Important rules for abstract methods in Java are:

  • An abstract method has no method body.
  • The declaration ends with a semicolon.
  • A class that contains at least one abstract method must be declared abstract.
  • A concrete subclass must implement all inherited abstract methods.
  • Abstract methods cannot be declared private, static, or final, because subclasses must be able to override them.

Java Abstract Class Syntax and Compiler Rules

An abstract class is a class that is declared abstract. And abstract class can have none or one or more abstract methods. But if there is at least one abstract method in a class, the class has to be declared abstract. And an imported point to be noted is that an abstract class can never be instantiated, i.e., an object of abstract class type can never be created.

An abstract class can only be a super class for other normal classes or other abstract classes.

Unlike an interface, an abstract class can keep shared state using fields and can define constructors. The constructor is not used to create an object of the abstract class directly; it runs when a subclass object is created.

</>
Copy
abstract class ClassName {
    abstract void methodName();
}

If you try to instantiate an abstract class directly, Java reports a compilation error.

</>
Copy
Vehicle vehicle = new Vehicle(); // Not allowed

Example 1 – Java Abstract Class with Abstract Methods

An example of an abstract class, and a sub-class that extends the abstract class, and an abstract class extending the abstract class will be shown below:

Vehicle is an abstract class with abstract methods accelerate() and breaking().

Vehicle.java

</>
Copy
public abstract class Vehicle {
	public abstract void accelerate();
	public abstract void breaking();
}

In this class, Vehicle defines the operations that every vehicle subclass must provide. The method name breaking() is kept as used in this example; in real projects, brake() or applyBrake() would be clearer names.

Example 2 – Abstract Class Extending Another Abstract Class in Java

We can extend an abstract class with another abstract class. Sub-class extends super class.

In the following program, Car is an abstract class, extending another abstract class, Vehicle, and has another abstract method mode().

Car.java

</>
Copy
public abstract class Car extends Vehicle {

	@Override
	public abstract void accelerate();

	@Override
	public abstract void breaking();

	public abstract void mode();

}

Since Car is also abstract, it does not have to provide method bodies for accelerate() and breaking(). It can also introduce a new abstract method, mode(), that concrete subclasses must implement later.

Example 3 – Concrete Java Class Extending an Abstract Class

We can extend a class with an abstract class. In such cases, we have to implement all the abstract methods and properties of the abstract class.

In this following program, Jaguar is a class extending abstract class Car and implementing all the abstract methods in Car.

Jaguar.java

</>
Copy
public class Jaguar extends Car {

	String name;

	public Jaguar(String name){
		this.name = name;
	}

	@Override
	public void accelerate() {
		System.out.println(name + " is accelerating.");
	}

	@Override
	public void breaking() {
		System.out.println(name + " is accelerating.");
	}

	@Override
	public void mode() {
		System.out.println("The "+name+" is in sports mode.");
	}
}

Jaguar is a concrete class, so it must implement accelerate(), breaking(), and mode(). After that, we can create an object of Jaguar and call these methods.

AbstractClassDemo.java

</>
Copy
/**
 * This class provides a demo for Abstract class and methods.
 * @author tutorialkart.com
 *
 */
public class AbstractClassDemo {
	public static void main(String[] args) {
		System.out.println("---Demo on Interfaces in Java---");
		Jaguar myNewJaguar = new Jaguar("XJ");
		myNewJaguar.accelerate();
		myNewJaguar.breaking();
		myNewJaguar.mode();
	}
}

Output

---Demo on Interfaces in Java---
XJ is accelerating.
XJ is accelerating.
The XJ is in sports mode.

The first line in the output is printed by the demo program text. The inheritance structure in this example still demonstrates abstract classes and abstract methods.

Abstract Class with Concrete Methods and Shared Fields in Java

An abstract class does not have to contain only abstract methods. It can also define reusable code that all subclasses inherit. This is one of the main reasons to choose an abstract class instead of a normal interface.

</>
Copy
abstract class Payment {
    protected double amount;

    Payment(double amount) {
        this.amount = amount;
    }

    void printAmount() {
        System.out.println("Amount: " + amount);
    }

    abstract void pay();
}

class CardPayment extends Payment {
    CardPayment(double amount) {
        super(amount);
    }

    @Override
    void pay() {
        System.out.println("Paid using card.");
    }
}

In this example, Payment stores the common amount field and provides the concrete printAmount() method. The pay() method is abstract because each payment type may process payment differently.

Class vs Abstract Class vs Interface in Java

A normal class, an abstract class, and an interface can all be used in object-oriented design, but they serve different purposes. The following comparison helps decide which one fits a Java design.

Java typeCan be instantiated directly?Can contain abstract methods?Best use case
Normal classYesNoUse when the class is complete and objects can be created from it.
Abstract classNoYesUse when related subclasses share common fields or methods but must implement some behavior themselves.
InterfaceNoYesUse when unrelated classes need to follow the same capability contract.

When to Use Abstract Class vs Normal Class in Java

Use a normal class when the class is complete enough to create objects directly. Use an abstract class when the base type is only a partial design and needs subclasses to complete selected behavior.

  • Use Vehicle as an abstract class when every vehicle has common behavior but each vehicle type accelerates differently.
  • Use Car as a normal class when it has complete behavior and can be instantiated directly.
  • Use an interface when the design only needs a capability, such as Printable, Runnable, or Comparable.

Common Mistakes with Abstract Methods and Classes in Java

  • Adding a body to an abstract method: An abstract method declaration must not contain method body braces.
  • Forgetting the abstract keyword on the class: If a class has an abstract method, the class must also be declared abstract.
  • Trying to create an object of an abstract class: Java does not allow direct instantiation of abstract classes.
  • Not implementing all inherited abstract methods: A concrete subclass must implement every inherited abstract method.
  • Using abstract class when interface is enough: Prefer an interface when there is no shared state or common base implementation.

QA Checklist for Java Abstract Method and Abstract Class Examples

  • Check that each abstract method ends with a semicolon and has no method body.
  • Check that any class containing an abstract method is declared with the abstract keyword.
  • Check that each concrete subclass implements all inherited abstract methods.
  • Check that the tutorial does not create an object directly from an abstract class.
  • Check whether the example really needs shared fields or common methods; otherwise, an interface may be a better fit.

FAQs on Abstract Methods and Classes in Java

What is the main purpose of an abstract method in Java?

The main purpose of an abstract method is to force subclasses to provide a specific method implementation. It defines the method name, return type, and parameters, but leaves the body to the subclass.

Can an abstract class in Java have no abstract methods?

Yes. An abstract class can have no abstract methods. This is useful when you want to prevent direct object creation while still providing common fields, constructors, or methods for subclasses.

Can an abstract class have a constructor in Java?

Yes. An abstract class can have a constructor. The constructor runs when a subclass object is created and usually initializes common fields defined in the abstract class.

Does a subclass have to implement all abstract methods in Java?

A concrete subclass must implement all inherited abstract methods. If it does not implement them, the subclass must also be declared abstract.

When should I use an abstract class instead of an interface in Java?

Use an abstract class when related classes need shared fields, constructors, or common method implementations. Use an interface when you mainly need a common capability contract across different class hierarchies.

Summary of Java Abstract Methods and Abstract Classes

Abstract methods and classes in Java are used to model incomplete base types. An abstract method declares required behavior without implementation, and an abstract class provides a base for concrete subclasses. For more detail on the Java language rules, you may also refer to Oracle’s Java tutorial on abstract methods and classes.

In this Java Tutorial, we have completed the ways of implementing abstraction in Java. In our next Java tutorial, we shall learn Encapsulation in Java.