Abstract methods let you define a method without giving its details. This means subclasses must provide their own version of the method.
Abstract methods in Java
abstract class ClassName { abstract returnType methodName(parameters); }
The class containing an abstract method must be declared abstract.
Abstract methods have no body (no curly braces, just a semicolon).
abstract class Animal { abstract void makeSound(); }
abstract class Shape { abstract double area(); }
abstract class Vehicle { abstract void startEngine(); } class Car extends Vehicle { @Override void startEngine() { System.out.println("Car engine started"); } }
This program shows an abstract class Animal with an abstract method makeSound. Dog and Cat classes provide their own sounds. The sleep method is shared.
abstract class Animal { abstract void makeSound(); void sleep() { System.out.println("Animal is sleeping"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Dog barks"); } } class Cat extends Animal { @Override void makeSound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.makeSound(); dog.sleep(); cat.makeSound(); cat.sleep(); } }
Time complexity: Abstract methods themselves do not affect time complexity; it depends on the subclass implementation.
Space complexity: No extra space is used by abstract methods alone.
Common mistake: Forgetting to implement all abstract methods in subclasses causes compile errors.
Use abstract methods when you want to force subclasses to provide specific behavior, unlike normal methods that can have default behavior.
Abstract methods have no body and must be implemented by subclasses.
Classes with abstract methods must be declared abstract.
Abstract methods help design flexible and reusable code by defining a contract for subclasses.