Understanding Java's Abstract Keyword with Examples

Répondu en utilisant GPT-4o-mini par Java Q&A Hub

Question :

Introduce Java abstract Keyword and give examples

Réponse :

In Java, the abstract keyword is used to declare a class or a method that is incomplete and must be implemented by subclasses. It is a fundamental concept in object-oriented programming that supports abstraction and helps in defining a common interface for different classes.

Abstract Classes

An abstract class cannot be instantiated on its own and may contain abstract methods (methods without a body) as well as concrete methods (methods with a body). Subclasses that extend an abstract class must provide implementations for all abstract methods.

Example of an Abstract Class

// Abstract class
abstract class Animal {
    // Abstract method (does not have a body)
    abstract void sound();

    // Concrete method
    void sleep() {
        System.out.println("Sleeping...");
    }
}

// Subclass that extends the abstract class
class Dog extends Animal {
    // Implementing the abstract method
    void sound() {
        System.out.println("Bark");
    }
}

// Subclass that extends the abstract class
class Cat extends Animal {
    // Implementing the abstract method
    void sound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.sound(); // Output: Bark
        myDog.sleep(); // Output: Sleeping...

        myCat.sound(); // Output: Meow
        myCat.sleep(); // Output: Sleeping...
    }
}

Abstract Methods

An abstract method is a method that is declared without an implementation. It must be implemented by any non-abstract subclass.

Example of an Abstract Method

abstract class Shape {
    // Abstract method
    abstract void draw();
}

class Circle extends Shape {
    // Implementing the abstract method
    void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Rectangle extends Shape {
    // Implementing the abstract method
    void draw() {
        System.out.println("Drawing a Rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle();
        Shape rectangle = new Rectangle();

        circle.draw(); // Output: Drawing a Circle
        rectangle.draw(); // Output: Drawing a Rectangle
    }
}

Key Points

  1. Instantiation: You cannot create an instance of an abstract class directly.
  2. Subclasses: Any subclass of an abstract class must implement all abstract methods unless the subclass is also abstract.
  3. Concrete Methods: Abstract classes can have concrete methods that can be used by subclasses.
  4. Polymorphism: Abstract classes allow for polymorphism, enabling you to use a superclass reference to refer to subclass objects.

Using the abstract keyword effectively allows you to define a common interface for a group of related classes while leaving the implementation details to the subclasses. This promotes code reusability and a cleaner design.