G.2 Superclasses and Subclasses
Often, an object of one class is an object of another class as well. Figure G.1 lists several examples of superclasses and subclasses—superclasses tend to be “more general” and subclasses “more specific.” For example, a CarLoan
is a Loan
as are HomeImprovementLoan
s and MortgageLoan
s. Thus, in Java, class CarLoan
can be said to inherit from class Loan
. In this context, class Loan
is a superclass and class CarLoan
is a subclass. A CarLoan
is a specific type of Loan
, but it’s incorrect to claim that every Loan
is a CarLoan
—the Loan
could be any type of loan.
Fig. G.1 Inheritance examples
Superclass | Subclasses |
---|---|
Student |
GraduateStudent , UndergraduateStudent |
Shape |
Circle , Triangle , Rectangle , Sphere , Cube |
Get Android How to Program, 3/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.