Inheritance
Inheritance is a mechanism where a class (subclass) acquires the fields and methods of another class (superclass). Declared with the extends keyword. Java supports single class inheritance but multiple interface implementation.
Example
public class Vehicle {
protected int wheels;
public Vehicle(int wheels) { this.wheels = wheels; }
public void start() { System.out.println("Engine started"); }
}
public class Car extends Vehicle {
private String brand;
public Car(String brand) {
super(4); // call superclass constructor
this.brand = brand;
}
@Override
public void start() {
super.start(); // call the parent's version
System.out.println(brand + " is ready");
}
}
Constructor chaining with super()
The first statement in any subclass constructor is always a call to super(...). If you don't write one, the compiler inserts an implicit super() calling the parent's no-arg constructor. If the parent has no no-arg constructor, you must call super(...) explicitly.
Overriding
A subclass can override a non-final instance method with the same signature, annotated with @Override. The subclass version runs when the method is called on an instance, regardless of the declared reference type — this is polymorphism in action.
Composition over inheritance
The Gang of Four design principle: favour composition over inheritance. Deep inheritance hierarchies are fragile and hard to refactor. Prefer interface-based design with composed dependencies.