What is an abstract class?

Abstract Classes in Programming: A Simple Guide

Ever built a game with different characters, each with unique abilities? That's where abstract classes shine! They're like blueprints for objects, providing a basic structure without all the specific details. This post will explain what they are, when to use them, and how they differ from interfaces.

What is an Abstract Class?

An abstract class is a special type of class that can't be directly instantiated (you can't create objects of it). It acts as a template or parent class for other classes (called subclasses). Think of it as an incomplete recipe – it outlines the basic ingredients and steps, but leaves some parts for specific recipes (subclasses) to fill in.

The key feature is abstract methods. These are methods declared within the abstract class but without any implementation. Subclasses are required to provide the concrete implementation of these abstract methods.

Example in Java


abstract class Shape {
  abstract double getArea(); // Abstract method
  void printName() { System.out.println("This is a shape"); } // Concrete method
}

class Circle extends Shape {
  double radius;
  Circle(double r) { radius = r; }
  double getArea() { return Math.PI * radius * radius; } // Implementation of abstract method
}

Here, `Shape` is an abstract class with an abstract `getArea()` method. The `Circle` class extends `Shape` and provides the implementation for `getArea()`.

The abstract keyword is used to define an abstract class or method in Java (and similar keywords exist in other languages like C# and Python).

When to Use Abstract Classes

Abstract classes are useful in many situations:

Scenario 1: Creating a Blueprint

Imagine modeling different shapes (circles, squares, triangles). An abstract `Shape` class can define a common `getArea()` method, leaving the specific calculation to each subclass.

Scenario 2: Defining a Common Interface (But With Some Implementation)

Different account types in a bank (savings, checking) can share common methods like `deposit()` and `withdraw()`, but might have different implementations for things like interest calculations. An abstract `Account` class could handle the common parts.

Benefits:

  • Code Reusability: Common code resides in the abstract class.
  • Maintainability: Changes in the abstract class propagate to subclasses.
  • Extensibility: Easy to add new subclasses without modifying existing code.

Abstract Classes vs. Interfaces

Both abstract classes and interfaces define a common structure, but there are key differences:

Feature Abstract Class Interface
Method Implementation Can have both abstract and concrete methods Only abstract methods (Java, C#); default methods are allowed (Java 8 onwards)
Inheritance Single inheritance (in most languages) Multiple inheritance allowed
Member Variables Can have instance variables Generally cannot have instance variables

Choose abstract classes when you want a common structure with some default implementation. Choose interfaces for purely abstract contracts.

Conclusion

Abstract classes are powerful tools for building flexible and maintainable object-oriented code. By providing a common framework with some default behavior, they improve code reusability and help you create well-organized and robust applications. Try them out in your next project!