Java Programming Handbook

    Abstract Classes in Java

    Introduction#

    In Java, sometimes we want to create a class that should not be instantiated directly but instead should serve as a blueprint for other classes. This is where abstract classes come into play!

    An abstract class is a class that cannot be instantiated and may contain abstract methods (methods without a body) that must be implemented by its subclasses.

    Think of an abstract class as a template—it provides some functionality but leaves specific details for subclasses to define.

    Understanding Abstract Classes with a Real-World Example#

    Imagine you are developing a program for different types of vehicles. All vehicles share some common properties (like speed, fuel type) but have different ways of moving (cars drive, planes fly, boats sail).

    Here, you can create an abstract class Vehicle that has general properties and methods but leaves the implementation of move() to specific vehicle types.

    // Abstract class abstract class Vehicle { int speed; String fuelType; // Constructor Vehicle(int speed, String fuelType) { this.speed = speed; this.fuelType = fuelType; } // Abstract method (to be implemented by subclasses) abstract void move(); // Concrete method (common functionality for all vehicles) void showInfo() { System.out.println("Speed: " + speed + " km/h"); System.out.println("Fuel Type: " + fuelType); } } // Subclass Car class Car extends Vehicle { Car(int speed, String fuelType) { super(speed, fuelType); } @Override void move() { System.out.println("Car moves on roads."); } } // Subclass Airplane class Airplane extends Vehicle { Airplane(int speed, String fuelType) { super(speed, fuelType); } @Override void move() { System.out.println("Airplane flies in the sky."); } } // Main class to test public class AbstractExample { public static void main(String[] args) { Vehicle car = new Car(120, "Petrol"); car.showInfo(); car.move(); System.out.println(); Vehicle airplane = new Airplane(800, "Jet Fuel"); airplane.showInfo(); airplane.move(); } }

    Output:#

    Speed: 120 km/h Fuel Type: Petrol Car moves on roads. Speed: 800 km/h Fuel Type: Jet Fuel Airplane flies in the sky.

    Key Features of Abstract Classes#

    Cannot be instantiated – You cannot create an object of an abstract class. ✔ Can have abstract methods – Methods without implementation that subclasses must define. ✔ Can have concrete methods – Fully implemented methods that all subclasses inherit. ✔ Can have constructors – Used to initialize fields, just like a normal class. ✔ Can contain variables – Both instance and static variables are allowed.

    Do’s and Don’ts of Abstract Classes (with Examples)#

    Do’s#

    1. Use abstract classes when you want to enforce common functionality#

    If multiple classes share some behavior but should also have their unique implementations, an abstract class is a great choice.

    abstract class Animal { abstract void makeSound(); void sleep() { System.out.println("Sleeping..."); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Barks"); } }

    2. Use abstract classes when you need a constructor for common properties#

    Unlike interfaces, abstract classes can have constructors to initialize common attributes.

    abstract class Employee { String name; int salary; Employee(String name, int salary) { this.name = name; this.salary = salary; } }

    Don’ts#

    1. Don’t instantiate an abstract class#

    Abstract classes are meant to be extended, not instantiated.

    Incorrect:

    abstract class Shape {} public class Test { public static void main(String[] args) { Shape s = new Shape(); // ERROR: Cannot instantiate abstract class } }

    2. Don’t declare an abstract method in a class that can be instantiated#

    If a class has an abstract method, the class itself must be abstract.

    Incorrect:

    class Vehicle { // Should be abstract abstract void move(); // ERROR: Must declare class as abstract }

    Correct:

    abstract class Vehicle { abstract void move(); }

    3. Don’t forget to override abstract methods in subclasses#

    If a subclass does not implement an abstract method, it must also be declared abstract.

    Incorrect:

    abstract class Animal { abstract void sound(); } class Dog extends Animal {} // ERROR: Must implement sound() method

    Correct:

    class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } }

    Conclusion#

    In this blog, we learned about Abstract Classes in Java. We covered:

    • What abstract classes are and why they are useful.
    • A real-world example demonstrating how to use them.
    • Key do’s and don’ts with explanations and examples.
    • Differences between abstract classes and interfaces.

    Abstract classes help in structuring your code by enforcing a common design while allowing flexibility for subclasses. Understanding them will make your object-oriented programming skills stronger.

    Last updated on Apr 09, 2025