Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the spectra-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home4/mahidhar/public_html/wp-includes/functions.php on line 6114
Abstract Classes in Java | tutorialQ

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