Java Reference
In-Depth Information
437
Chapter 10 Inheritance
C HAPTER G OALS
ȗ To learn about inheritance
ȗ To understand how to inherit and override superclass methods
ȗ To be able to invoke superclass constructors
ȗ To learn about protected and package access control
ȗ To understand the common superclass Object and how to override its toString
and equals methods
G To use inheritance for customizing user interfaces
In this chapter, we discuss the important concept of inheritance. Specialized classes
can be created that inherit behavior from more general classes. You will learn how to
implement inheritance in Java, and how to make use of the Object classȌthe most
general class in the inheritance hierarchy.
437
438
10.1 An Introduction to Inheritance
Inheritance is a mechanism for enhancing existing classes. If you need to implement
a new class and a class representing a more general concept is already available, then
the new class can inherit from the existing class. For example, suppose you need to
define a class SavingsAccount to model an account that pays a fixed interest rate
on deposits. You already have a class BankAccount , and a savings account is a
special case of a bank account. In this case, it makes sense to use the language
construct of inheritance. Here is the syntax for the class definition:
Inheritance is a mechanism for extending existing classes by adding methods and
fields.
438
439
class SavingsAccount extends BankAccount
{
Search WWH ::




Custom Search