Inheritance: The Car subclass

public class Car extends MotorVehicle {

  protected int numberWheels = 4;  
  protected int numberDoors;
  
  
  // constructors
  public Car(String licensePlate, double maxSpeed,
   String make, String model, int year, int numberOfPassengers,
   int numberOfDoors) {
    this(licensePlate, maxSpeed, make, model, year, numberOfPassengers, 
     numberOfDoors);    
  }

  public Car(String licensePlate, double speed, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {
    this(licensePlate, speed, maxSpeed, make, model, year, 
     numberOfPassengers, 4);    
  }

  public Car(String licensePlate, double speed, double maxSpeed,
   String make, String model, int year, int numberOfPassengers,
   int numberOfDoors) {
    super(licensePlate, speed, maxSpeed, make, model,
     year, numberOfPassengers);
    this.numberDoors = numberOfDoors;
  }
   
  public int getNumberOfWheels() {
    return this.numberWheels;
  }
 
  public int getNumberOfDoors() {
    return this.numberDoors;
  }
    
}
It may look like these classes aren't as complete as the earlier ones, but that's incorrect. Car and Motorcycle each inherit the members of their superclass, MotorVehicle. Since a MotorVehicle has a make, a model, a year, a speed, a maximum speed, a number of passengers, cars and motorcycles also have makes, models, years, speeds, maximum speeds, and numbers of passengers. They alse have all the public methods the superclass has. They do not have the same constructors, though they can invoke the superclass constructor through the super keyword, much as a constructor in the same class can be invoked with the this keyword.


Previous | Next | Top
Last Modified February 3, 1999
Copyright 1997-1999 Elliotte Rusty Harold
elharo@metalab.unc.edu