Abstract Classes and Interfaces
Learn about abstract classes, interfaces, and their role in achieving abstraction and defining contracts.
Abstract Classes in Java
What are Abstract Classes?
In Java, an abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes, providing a common interface and potentially implementing some shared behavior. The primary purpose of an abstract class is to be inherited by other classes (derived classes or subclasses).
Abstract classes are declared using the abstract
keyword.
Key Properties of Abstract Classes
- Cannot be Instantiated: You cannot create an object of an abstract class directly.
new AbstractClass()
will result in a compilation error. - May Contain Abstract Methods: An abstract method is a method declared without an implementation. It's essentially a declaration, leaving the implementation to its subclasses. Abstract methods are also declared using the
abstract
keyword. If a class contains even one abstract method, the entire class *must* be declared as abstract. - Can Contain Concrete (Non-Abstract) Methods: Abstract classes can contain regular methods with implementations, providing common functionality to subclasses.
- Must be Extended: To use an abstract class, you must create a subclass (derived class) that extends it.
- Subclasses Must Implement Abstract Methods: If a subclass of an abstract class does *not* implement all of the abstract methods of its superclass, then the subclass *must* also be declared as abstract.
How Abstract Classes Contribute to Abstraction
Abstract classes are a core feature of abstraction in object-oriented programming. Abstraction allows you to focus on the essential characteristics of an object while hiding unnecessary details. Abstract classes enforce a particular interface (set of methods) that subclasses must implement, guaranteeing a certain level of consistency across derived classes.
Here's how they contribute:
- Defining a Common Interface: Abstract classes define a common interface for a group of related classes. All subclasses are guaranteed to have a specific set of methods, making them interchangeable in certain contexts.
- Enforcing a Hierarchy: They establish a clear hierarchy of classes, promoting code organization and maintainability.
- Code Reusability: Abstract classes allow you to reuse common code in the abstract class's non-abstract methods, reducing code duplication.
- Polymorphism: Abstract classes are crucial for achieving polymorphism. You can treat objects of different subclasses as objects of the abstract superclass, simplifying code and making it more flexible.
Java Example
Here's a simple example of an abstract class in Java:
abstract class Shape {
// Abstract method (no implementation)
public abstract double getArea();
// Concrete method (with implementation)
public void displayShapeType() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Circle Area: " + circle.getArea());
rectangle.displayShapeType();
System.out.println("Rectangle Area: " + rectangle.getArea());
}
}
In this example:
Shape
is an abstract class with an abstract methodgetArea()
and a concrete methoddisplayShapeType()
.Circle
andRectangle
are subclasses ofShape
that implement thegetArea()
method.- The
Main
class demonstrates how to create instances of the subclasses and call their methods.
Key takeaways from the example:
- The
Shape
class cannot be instantiated.Shape shape = new Shape();
would cause an error. - The
Circle
andRectangle
classes *must* implement thegetArea()
method because it is declared abstract in theShape
class. - The
displayShapeType()
method is inherited by bothCircle
andRectangle
and can be used directly or overridden.
When to Use Abstract Classes
Consider using abstract classes in the following situations:
- When you want to define a common interface for a group of related classes.
- When you want to provide a partial implementation that subclasses can inherit and extend.
- When you want to prevent direct instantiation of a base class.