Inheritance: the Motorcycle subclass

The MotorVehicle class has all the characteristics shared by motorcycles and cars, but it leaves the number of wheels unspecified, and it doesn't have a numberDoors field since not all motor vehicles have doors.

Next you define two subclasses of MotorVehicle, one for cars and one for motorcycles. To do this you use the keyword extends.

public class Motorcycle extends MotorVehicle {

  private int numberWheels = 2;  
  
  
  // constructors
  public Motorcycle(String licensePlate, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {
    this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers);    
  }

  public Motorcycle(String licensePlate, double speed, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {
   
    // invoke superclass constructor
    super(licensePlate, speed, maxSpeed, make, model, year, 
     numberOfPassengers);   
  }
  
  public int getNumberOfWheels() {
    return this.numberWheels;
  }
    
}

Previous | Next | Top | Cafe au Lait

Copyright 1997-1999, 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 30, 2003