Abstract Classes in Java

Definition: An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain abstract methods (without implementation) and concrete methods (with implementation).
Purpose: Abstract classes provide a common base and partial implementation for subclasses.

Key Points:

  • An abstract class can have instance variables, constructors, and methods (both abstract and concrete).
  • A subclass that extends an abstract class must implement all abstract methods of the superclass.
  • A class can extend only one abstract class.
Java
abstract class Game {
    abstract void start();
    
    void end() {
        System.out.println("Game ended.");
    }
}

class Football extends Game {
    void start() {
        System.out.println("Football game started.");
    }
}

class Basketball extends Game {
    void start() {
        System.out.println("Basketball game started.");
    }
}

public class AbstractClassExample {
    public static void main(String[] args) {
        Game football = new Football();
        Game basketball = new Basketball();
        football.start();   // Output: Football game started.
        football.end();     // Output: Game ended.
        basketball.start(); // Output: Basketball game started.
        basketball.end();   // Output: Game ended.
    }
}
Scroll to Top