Skip to content
Keyhan Hadjari edited this page Sep 17, 2016 · 1 revision

Inheritance

  • Subclass call superclass constructor by super(xxx)
  • The identifier super(xxx) has to be the first line in subclass constructor
  • The subclass constructor has to call at least one of the superclass constructors, default constructor is only allowed if superclass has a no argument constructor or default constructor.
public class Vehicle {
    protected int numberOfWheels;
    protected int numberOfPassengers;

    public Vehicle(int nWheels, nPassengers) {
    }

}

public class Motorcycle extends Vehicle{
    public Motorcycle(EngineType type) {
        super(2,2);
        .
        .
    }
}

public class Minivan extends Vehicle{
    public Minivan(String brand) {
        super(4,7);
        .
        .
    }
}

Clone this wiki locally