Abstract Class in Java | OOP Concept in Java

An abstract class in Java is a class that cannot be instantiated and is used to declare common characteristics of subclasses. It is designed to be extended by other classes, allowing them to share methods and properties while also implementing their specific functionalities. Abstract classes provide a way to achieve partial abstraction in Java.

Key Points on Abstract Class:

Syntax of Abstract Class:

Syntax Example

abstract class Animal {
    abstract void sound(); // Abstract method

    void eat() { // Concrete method
        System.out.println("This animal eats food.");
    }
}

Example of Abstract Class in Java:

This example demonstrates an abstract class Animal with an abstract method sound() and a concrete method eat(). The Dog class extends Animal and provides its own implementation of sound().

Code Example

abstract class Animal {
    abstract void sound(); // Abstract method

    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks.");
    }
    
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound(); // Calls overridden method
        dog.eat(); // Calls inherited concrete method
    }
}

Output

Dog barks.
This animal eats food.

Detailed Explanation:

Abstract classes are essential for building a robust class hierarchy, allowing developers to define a common base structure while leaving specific details to be implemented by subclasses.