tight coupling - overuse of inheritance can lead to tightly coupled code leading to harder maintainability
fragile base case problem - changes to the superclass can unintentionally break subclasses
class Vehicle {
String brand;
int speed;
Vehicle(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
void move() {
System.out.println(brand + " is moving at " + speed + " km/h.");
}
}
class Car extends Vehicle {
int doors;
Car(String brand, int speed, int doors) {
super(brand, speed);
this.doors = doors;
}
@Override
void move() {
System.out.println(brand + " car with " + doors + " doors is moving at " + speed + " km/h.");
}
}
class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car("Toyota", 120, 4);
vehicle.move(); // Output: Toyota car with 4 doors is moving at 120 km/h.
}
}