What is an abstract class?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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 and Motorcycle are concrete subclasses that extend Vehicle and implement the startEngine() 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.

To deepen your understanding of abstract classes and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to become a successful developer?
What are the qualities of a good interviewer?
What is algorithm formula?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.