CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 145
Car two (carTwo) we shift into second, using .shiftGears(2), then .applyBrake(10) to slow it down
from 15 to 5 mph, and finally turn the car west, by using a .turnWheel("W") method call, all using
dot notation.
Dot notation “connects” the Java method call to the Java object, invoking that method on that Java
object. Once you understand it, you will see it is actually really cool how Java works!
We will be creating a custom class for your HelloUniverse Android app during the second half of this
chapter, and so you will be getting some excellent experience creating your own constructor method
very soon.
Extend an Object’s Structure: Java Inheritance
There is also support in Java for developing different types of custom objects, in this case it’s a Car
object, by using a technique called inheritance. Inheritance is where more specialized Car classes
(more uniquely defined Car objects) can be subclassed using the basic Car superclass. The
inheritance process is shown in Figure 5-2.
Figure 5-2. Inheritance of the Car Object superclass to create the SUV and the SPORT Car Object subclasses
Once a class is used for inheritance by “subclassing” it, it becomes a superclass. Ultimately, there
can be only one superclass at the very top of the chain, but there can be an unlimited number of
subclasses. All of the subclasses inherit the methods and fields from the superclass. The ultimate
example of this in Java SE is the java.lang.Object superclass (I call it the masterclass), which is
used to create all other classes in Java.
As an example of inheritance using our Car class, we could create the Suv subclass, using the Car
class as the “master” class. This is done using the Java “extends” keyword, which extends the
Car class definition, creating the Suv class definition. The Suv class will then define the attributes
(variables) and behaviors (methods), which then apply only to an SUV type of car, in addition to
extending all of those attributes (variables) and behaviors (methods) that apply to all types of
Car objects, which is the functionality that the extends keyword provides to the subclassing
(inheritance) operation.