Program 7.4: A Motor Vehicle Class

There's a lot of overlap between these two class definitions. OOP languages provide a mechanism for taking advantage of the overlap. This mechanism is inheritance. In this example you begin by defining a more general MotorVehicle class.

class MotorVehicle {

  String make;
  String model;
  int year;
  int max_speed;
  int weight
  float price;
  int num_passengers;
  int num_wheels;

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


Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home