
Inheritance in Java • 223
Given this declaration, a program could refer to ‘myCar.numberOfDoors’, since ‘numberOfDoors’ is an
instance variable in the class Car. But since class Car extends class Vehicle, a car also has all the structures
and behaviour of a Vehicle. This means that ‘myCar.registrationNumber’, ‘myCar.owner’ and ‘myCar.
transferOwnership()’ also exist.
Now, in the real world, cars, trucks and motorcycles are in fact vehicles. The same is true in a program.
That is, an object of type Car or truck or Motorcycle is automatically an object of type Vehicle. This brings
out the following important facts: A variable that can hold a ...