Abstract Class in Java

What is an Abstract Class in Java?

An Abstract Class is a class in Java that cannot be instantiated directly. It is used as a base class and is meant to be extended by other classes. Abstract classes can contain abstract methods (without implementation) and concrete methods (with implementation).

An abstract class helps in providing a common template to its child classes and enforcing some method implementation through abstraction, but still allowing common method implementations.

Why Use Abstract Classes?

  • Abstract classes allow partial implementation of functionality that is common to multiple subclasses, reducing code duplication.
  • They also help enforce method overriding by requiring subclasses to implement abstract methods.
  • By using abstract classes, you follow key OOP principles, such as Abstraction and promoting Code Reusability.

Abstract Class Syntax Example

// Abstract class
abstract class Animal {
    
    // Abstract method (must be implemented by subclass)
    abstract void sound();
    
    // Concrete method (common functionality)
    void eat() {
        System.out.println("This animal eats food");
    }
}

// Concrete subclass
class Dog extends Animal {
    
    // Implementation of abstract method
    void sound() {
        System.out.println("Dog barks");
    }
}

// Main class to run the program
public class AbstractClassDemo {
    public static void main(String[] args) {
        Animal myDog = new Dog();  // Animal reference, Dog object
        myDog.eat();               // Calls concrete method from abstract class
        myDog.sound();             // Calls overridden method
    }
}

Output:

This animal eats food
Dog barks

Key Points About Abstract Class

FeatureDescription
InstantiationCannot create object of abstract class directly
Abstract MethodsDeclared with abstract keyword; No method body
Concrete MethodsCan have implemented methods
ConstructorAbstract classes can have constructors
VariablesCan have instance variables
Access ModifiersAbstract methods can have public or protected access modifiers
Subclass ResponsibilityConcrete subclass must implement all abstract methods or itself be abstract

Abstract Class vs Interface

Abstract ClassInterface
Can have constructorsNo constructors (before Java 8)
Can have abstract + concrete methodsJava 8+: Can have default methods with implementation
Can maintain state (instance variables)No instance variables (except static and final)
Supports method access modifiers (private/protected/public)All methods are public (by default)
Used when classes are closely relatedUsed for defining capabilities or behavior across unrelated classes

When to Use Abstract Class?

  • Use an abstract class when a base class with common functionality should be shared by multiple subclasses.
  • It is helpful in cases where partial implementation is needed.
  • Abstract classes also improve code reusability while enforcing implementation rules in subclasses.

    Example of Abstract Class with Multiple Subclasses

    abstract class Shape {
        abstract void area();
        
        void display() {
            System.out.println("This is a shape");
        }
    }
    
    class Circle extends Shape {
        int radius = 5;
        
        void area() {
            System.out.println("Area of Circle: " + (3.14 * radius * radius));
        }
    }
    
    class Rectangle extends Shape {
        int length = 10, width = 5;
        
        void area() {
            System.out.println("Area of Rectangle: " + (length * width));
        }
    }
    
    public class ShapeDemo {
        public static void main(String[] args) {
            Shape c = new Circle();
            c.display();
            c.area();
            
            Shape r = new Rectangle();
            r.display();
            r.area();
        }
    }
    

    Output:

    This is a shape
    Area of Circle: 78.5
    This is a shape
    Area of Rectangle: 50
    

    Best Practices

    • Prefer abstract classes when classes share a strong relationship and common implementation.
    • Avoid creating large abstract classes with too many abstract methods.
    • Keep it clean: Abstract class should focus on providing a template and common functionality.

    Summary

    An abstract class in Java provides a blueprint for subclasses. It helps enforce rules by using abstract methods, while at the same time allowing code reuse through concrete methods. Moreover, it serves as a powerful tool when designing applications that follow key OOP principles, such as abstraction, inheritance, and polymorphism. In addition, abstract classes promote a clean and scalable architecture by clearly defining a template for subclasses.

    Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

    Leave a Reply

    Your email address will not be published. Required fields are marked *