What is an abstract class?
Understanding abstract classes is essential for mastering Object-Oriented Programming (OOP) and building flexible, maintainable software systems. Abstract classes serve as blueprints for other classes, enabling you to define common behaviors while allowing subclasses to provide specific implementations.
Abstract Class
An abstract class is a class that cannot be instantiated on its own and is designed to be subclassed. It serves as a template for other classes, providing a common structure and shared functionality while allowing subclasses to implement specific details. Abstract classes often include abstract methods, which are declared without an implementation and must be overridden by subclasses.
Example
Consider a scenario in a vehicle management system where you have different types of vehicles like cars, trucks, and motorcycles. You can create an abstract class Vehicle
that defines common attributes and methods, while each specific vehicle type implements its own unique behaviors.
// Abstract class public abstract class Vehicle { protected String brand; protected String model; public Vehicle(String brand, String model) { this.brand = brand; this.model = model; } // Abstract method public abstract void startEngine(); // Concrete method public void displayInfo() { System.out.println("Brand: " + brand + ", Model: " + model); } } // Subclass inheriting from Vehicle public class Car extends Vehicle { public Car(String brand, String model) { super(brand, model); } @Override public void startEngine() { System.out.println("Car engine started with a key."); } } // Subclass inheriting from Vehicle public class Motorcycle extends Vehicle { public Motorcycle(String brand, String model) { super(brand, model); } @Override public void startEngine() { System.out.println("Motorcycle engine started with a button."); } } // Usage public class Main { public static void main(String[] args) { Vehicle myCar = new Car("Toyota", "Corolla"); Vehicle myMotorcycle = new Motorcycle("Honda", "CBR500R"); myCar.displayInfo(); // Outputs: Brand: Toyota, Model: Corolla myCar.startEngine(); // Outputs: Car engine started with a key. myMotorcycle.displayInfo(); // Outputs: Brand: Honda, Model: CBR500R myMotorcycle.startEngine(); // Outputs: Motorcycle engine started with a button. } }
In this example:
Vehicle
is an abstract class that defines common attributes (brand
,model
) and methods (displayInfo()
,startEngine()
).startEngine()
is an abstract method, meaning each subclass must provide its own implementation.Car
andMotorcycle
are concrete subclasses that extendVehicle
and implement thestartEngine()
method differently.
Benefits of Using Abstract Classes
- Code Reusability: Abstract classes allow you to define common functionalities that can be reused by multiple subclasses, reducing code duplication.
- Flexibility: By enforcing subclasses to implement abstract methods, you ensure that each subclass provides its specific behavior while adhering to a common interface.
- Maintainability: Changes made to the abstract class automatically propagate to all subclasses, simplifying updates and maintenance.
- Organization: Abstract classes help in organizing code logically, making it easier to understand and manage complex systems.
Recommended Courses
To deepen your understanding of abstract classes and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the Coding Interview: Patterns for Coding Questions
- Grokking the System Design Interview
These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.
GET YOUR FREE
Coding Questions Catalog