Chapter 5.5.3 – Example Vehicles | Introduction to Programming Using Java

Chapter 5.5.3 – Example Vehicles | Introduction to Programming Using Java

 

5.5.3 Example Vehicles

 

Let’s look at an example. Suppose that a program has to deal with motor vehicles, including cars, trucks, and motorcycles. (This might be a program used by a Department of Motor Vehicles to keep track of registrations.) The program could use a class named Vehicle to represent all types of vehicles. Since cars, trucks, and motorcycles are types of vehicles, they would be represented by subclasses of the Vehicle class, as shown in this class hierarchy diagram:

 

Chapter 5.5.3 - Example Vehicles | Introduction to Programming Using Java

 

The Vehicle class would include instance variables such as registrationNumber and owner and instance methods such as transferOwnership(). These are variables and methods common to all vehicles. The three subclasses of Vehicle—Car, Truck, and Motorcycle—could then be used to hold variables and methods specific to particular types of vehicles. The Car class might add an instance variable numberOfDoors, the Truck class might have numberOfAxles, and the Motorcycle class could have a boolean variable hasSidecar.

(Well, it could in theory at least, even if it might give a chuckle to the people at the Department of Motor Vehicles.) The declarations of these classes in a Java program would look, in outline, like this (although in practice, they would probably be public classes, defined in separate files):

 

Chapter 5.5.3 - Example Vehicles | Introduction to Programming Using Java

 

Suppose that myCar is a variable of type Car that has been declared and initialized with the statement

 

Chapter 5.5.3 - Example Vehicles | Introduction to Programming Using Java

 

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 structure and behavior 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 too. This brings us to the following Important Fact:

 

A variable that can hold a reference to an object of class A can also hold a reference

to an object belonging to any subclass of A.

 

The practical effect of this in our example is that an object of type Car can be assigned to a variable of type Vehicle. That is, it would be legal to say

 

Chapter 5.5.3 - Example Vehicles | Introduction to Programming Using Java

 

After either of these statements, the variable myVehicle holds a reference to a Vehicle object that happens to be an instance of the subclass, Car. The object “remembers” that it is in fact a Car, and not just a Vehicle. Information about the actual class of an object is stored as part of that object. It is even possible to test whether a given object belongs to a given class, using the instanceof operator. The test:

 

Chapter 5.5.3 - Example Vehicles | Introduction to Programming Using Java

 

would be illegal because myVehicle could potentially refer to other types of vehicles that are not cars. This is similar to a problem we saw previously in Subsection 2.5.6: The computer will not allow you to assign an int value to a variable of type short, because not every int is a short. Similarly, it will not allow you to assign a value of type Vehicle to a variable of type Car because not every vehicle is a car.

As in the case of ints and shorts, the solution here is to use type-casting. If, for some reason, you happen to know that myVehicle does in fact refer to a Car, you can use the type cast (Car)myVehicle to tell the computer to treat myVehicle as if it were actually of type Car. So, you could say myCar = (Car)myVehicle;

and you could even refer to ((Car)myVehicle).numberOfDoors. As an example of how this could be used in a program, suppose that you want to print out relevant data about a vehicle. You could say:

 

example vehicles\

 

Note that for object types, when the computer executes a program, it checks whether type-casts are valid. So, for example, if myVehicle refers to an object of type Truck, then the type cast (Car)myVehicle would be an error. When this happes, an exception of type ClassCastException is thrown.

 

 

java

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5 – Objects and Classes | Introduction to Programming Using Java

Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java

Chapter 5.1.2 – Fundamentals of Objects | Introduction to Programming Using Java

Chapter 5.1.3 – Getters and Setters | Introduction to Programming Using Java

Chapter 5.2 – Constructors and Object Initialization | Introduction to Programming Using Java

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

33 thoughts on “Chapter 5.5.3 – Example Vehicles | Introduction to Programming Using Java”

Leave a Comment