Java Reference
In-Depth Information
model,year,and flag (Booleantrue/falsevalue)indicatingthatthetruckisanextended
cab. The first three System.out.println() method calls retrieve their pieces
of information by calling a Car or Truck instance's inherited getMake() ,
getModel() , and getYear() methods.
The final System.out.println() method call directly accesses the instance's
numWheels or isExtendedCab instance field. Although it's generally not a good
the Car and Truck class's main() methods, which provides this access, is present
Assuming that Listing 2-22 is stored in a file named , execute
javac to compile this source code into Vehicle.class ,
Car.class ,and Truck.class classfiles.Thenexecute java Car totestthe Car
class. This execution results in the following output:
Make = Toyota
Model = Camry
Year = 2011
Number of wheels = 4
Make = Aptera Motors
Model = Aptera 2e/2h
Year = 2012
Number of wheels = 3
Continuing,execute java Truck totestthe Truck class.Thisexecutionresultsin
the following output:
Make = Chevrolet
Model = Silverado
Year = 2011
Extended cab = true
Note Aclasswhoseinstancescannotbemodifiedisknownasan immutable class .
Vehicle isanexample.If Car 'sand Truck 's main() methods,whichcandirectly
read/write numWheels or isExtendedCab , were not present, Car and Truck
nor can it inherit private fields and methods. For example, Car does not inherit Ve-
hicle 'sconstructor,nordoesitinherit Vehicle 'sprivate make , model ,and year
Search WWH ::

Custom Search