Make your class hierarchy clear and flexible
TL;DR: Extract a common abstract class to mimic real-world structure.
Identify common behaviors in both classes.
Create an abstract class with shared behavior and no implementation.
Move common logic to the abstract class.
Update subclasses to inherit from the abstract class.
class Car {
void drive() {
System.out.println("Driving a car");
}
}
class Truck extends Car {
void load() {
System.out.println("Loading cargo");
}
void unload() {
System.out.println("Unloading cargo");
}
}
// Truck reuses driving method
// Overriding it would be another code smell
// Violating Liskov Substitution rule
abstract class Vehicle {
// 2. Create an abstract class
// with shared behavior and no implementation
abstract void drive();
// 1. Identify common behaviors in both classes
// 3. Move common logic to the abstract class
}
class Car extends Vehicle {
// 4. Update subclasses to inherit from the abstract class
void drive() {
System.out.println("Driving a car");
}
}
class Truck extends Vehicle {
// 4. Update subclasses to inherit from the abstract class
void drive() {
System.out.println("Driving a truck");
// Implementation is different than the car's
}
void load() {
System.out.println("Loading cargo");
}
void unload() {
System.out.println("Unloading cargo");
}
}
This refactoring is safe if you identify all common behaviors correctly and move one method at a time running the tests.
It reduces duplication, simplifies maintenance, and makes it easier to extend functionality by adding new concrete realizations.
By introducing an abstract class, the code better reflects the real-world hierarchy, creating a clear relationship between the generic and specific types.
Without Proper Instructions |
With Specific Instructions |
---|---|
This article is part of the Refactoring Series.