/ Backend Development

Java Abstraction explanation with example

Creator Image
Bashar Alshaibani
16 May 2024 -
5 min Reading time

In Java, abstract classes and methods provide a way to define methods that must be implemented by subclasses, while also allowing the abstract class to provide some default behavior. Here are some key points and examples to illustrate how to create and extend abstract classes in Java:

Key Points:

  1. Abstract Classes: Cannot be instantiated. They can have both abstract methods (methods without a body) and non-abstract methods (methods with a body).
  2. Abstract Methods: Declared without an implementation. Subclasses are required to provide implementations for these methods.
  3. Concrete Subclasses: The first concrete subclass (non-abstract subclass) must implement all inherited abstract methods.
  4. Access Modifiers: Abstract classes and methods cannot be marked as final or private.

Example:

Let's start by defining an abstract class with both abstract and non-abstract methods.

Abstract Class Definition:

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

    // Non-abstract method (has a body)
    void sleep() {
        System.out.println("Sleeping...");
    }
}

Concrete Subclass Implementation:

Next, we'll create a concrete subclass that extends the abstract class and implements the abstract method.

class Dog extends Animal {
    // Implement the abstract method
    @Override
    void makeSound() {
        System.out.println("Bark");
    }
}

class Cat extends Animal {
    // Implement the abstract method
    @Override
    void makeSound() {
        System.out.println("Meow");
    }
}

Using the Concrete Subclasses:

Finally, we can create instances of the concrete subclasses and use them.

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

        myDog.makeSound(); // Outputs "Bark"
        myDog.sleep();     // Outputs "Sleeping..."

        myCat.makeSound(); // Outputs "Meow"
        myCat.sleep();     // Outputs "Sleeping..."
    }
}

Explanation:

  1. Abstract Class (Animal): The Animal class is abstract and defines an abstract method makeSound() and a concrete method sleep().
  2. Concrete Subclasses (Dog and Cat): The Dog and Cat classes extend the Animal class and provide implementations for the makeSound() method. They inherit the sleep() method as it is.
  3. Using Subclasses: In the Test class, instances of Dog and Cat are created. The makeSound() method is called on each instance, demonstrating polymorphism. The sleep() method is also called, showing inheritance of non-abstract methods.

Important Rules and Considerations:

  • Abstract Class: Cannot be instantiated directly. Attempting to create an instance of an abstract class will result in a compile-time error.
  • Abstract Method: Must be implemented by the first concrete subclass. Failure to do so will result in a compile-time error.
  • Access Modifiers: Abstract classes and methods cannot be marked as final or private because they are meant to be extended and implemented by subclasses.

By following these rules and utilizing abstract classes and methods, you can define clear and flexible APIs in Java, ensuring that certain behaviors are enforced while allowing for specific implementations in subclasses.