Java Reference
In-Depth Information
new methods
new instance fields
}
In the SavingsAccount class definition you specify only new methods and
instance fields. The SavingsAccount class automatically inherits all methods and
instance fields of the BankAccount class. For example, the deposit method
automatically applies to savings accounts:
SavingsAccount collegeFund = new SavingsAccount(10);
// Savings account with 10% interest
collegeFund.deposit(500);
// OK to use BankAccount method with SavingsAccount object
We must introduce some more terminology here. The more general class that forms
the basis for inheritance is called the superclass. In our example, BankAccount is
the superclass and SavingsAccount is the subclass.
The more general class is called a superclass. The more specialized class that
inherits from the superclass is called the subclass.
In Java, every class that does not specifically extend another class is a subclass of the
class Object . For example, the BankAccount class extends the class Object .
The Object class has a small number of methods that make sense for all objects,
such as the toString method, which you can use to obtain a string that describes
the state of an object.
Every class extends the Object class either directly or indirectly.
Figure 1 is a class diagram showing the relationship between the three classes
Object , BankAccount , and SavingsAccount . In a class diagram, you denote
inheritance by a solid arrow with a Ȓhollow triangleȓ tip that points to the superclass.
Search WWH ::




Custom Search