What happens if a class includes an abstract method?

If a class contains at least one abstract method, the class itself must be declared as abstract. An abstract method defines only the method signature without providing an implementation. The actual implementation must be provided by the subclass that extends the abstract class.

Key Points: • A class containing an abstract method must be declared using the abstract keyword. • Abstract classes cannot be instantiated directly. • Subclasses are required to implement all inherited abstract methods unless they are also declared abstract. • Abstract methods help enforce a common contract across related classes. • Abstract classes can contain both abstract and concrete methods.

Example: Consider a Vehicle class that defines a start() method but does not specify how each vehicle starts. Different vehicle types such as Car and Bike can provide their own implementations.

Code Example:

abstract class Vehicle {

    abstract void start();

    void stop() {
        System.out.println("Vehicle Stopped");
    }
}

class Car extends Vehicle {

    @Override
    void start() {
        System.out.println("Car Started");
    }
}

public class Demo {

    public static void main(String[] args) {

        Vehicle vehicle = new Car();

        vehicle.start();
        vehicle.stop();
    }
}

Output:

Car Started Vehicle Stopped

What Happens If a Subclass Does Not Implement the Abstract Method?

abstract class Vehicle {

    abstract void start();
}

class Car extends Vehicle {

}

Compilation Error:

Car is not abstract and does not override abstract method start()

Benefits of Abstract Methods:

• Enforce a common contract • Support abstraction • Promote code consistency • Enable Runtime Polymorphism • Improve application design flexibility

Interview Tip: A concise interview answer is:

"If a class contains an abstract method, the class must be declared abstract. Such a class cannot be instantiated directly, and any concrete subclass must provide implementations for all inherited abstract methods."