> Inheriting Instance Fields and Methods ~ Online tutorial

Inheriting Instance Fields and Methods

Inheriting Instance Fields and Methods
When you form a subclass of a given class, you can specify additional instance fields
and methods. In this section we will discuss this process in detail.
When defining the methods for a subclass, there are three possibilities.
1. You can Override methods from the superclass. If you specify a method with
the same signature (that is, the same name and the same parameter types), it
overrides the method of the same name in the superclass. Whenever the method
is applied to an object of the subclass type, the overriding method, and not the
original method, is executed. For example, CheckingAccount.deposit
overrides BankAccount.deposit.
2. You can inherit methods from the superclass. If you do not explicitly override a
superclass method, you automatically inherit it. The superclass method can be
applied to the subclass objects. For example, the SavingsAccount class
inherits the BankAccount.getBalance method.
3. You can define new methods. If you define a method that did not exist in the
superclass, then the new method can be applied only to subclass objects. For
example, SavingsAccount.addInterest is a new method that does not
exist in the superclass BankAccount.

Buy It now


Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: