Upload
egbert-pitts
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Inheritance
One of the biggest advantages of object-oriented
design is that of inheritance. A class may be
derived from another class, the base class. The
derived class inherits the fields and methods of the
class. The derived class may have additional fields
and methods, and may replace the methods that it
inherits.
Benefits of Inheritance
Inheritance allows the programmer to write simpler,
smaller code. If there are two or more classes that
are similar (but not exactly the same), all the
commonalities can be put into a single base class.
The classes are then derived classes of the base
class. Only the differences between the derived
classes and the base classes need to be coded.
Example: Bank Account
Suppose the we have different types of bank
accounts: Checking account Checking account with interest Checking with overdraft protection Savings account Medical spending account
Example (cont'd)
The fields and methods that are common to all
accounts can be put into a single base class. This
might include: Balance Transaction log Deposit Withdraw Check Balance
Example (cont'd)
The various account class would be derived from the
base class and would inherit the common fields
and methods. We could then add additional fields
and methods to customize each derived class. For
example, a checking account with interest might
have an APR field and a method to add interest to
the balance.
Overiding Methods
In addition to new methods, a derived class can
replace the methods of the base class. This is called
overriding a method. For example, the savings
account might have restriction on the number of
withdrawals that can be made in a month. So, the
withdraw method would be different for the
savings account class.
Syntax
To create a derived class, we use the keyword
extends in the class heading followed by the base
class. For example:
public class SavingAcct extends BankAccount {
}
Hierarchy of Classes
A derived class may also be the base class for
another derived class. That is, you can have a chain
of derived class. There is a hierarchy of classes,
that is, a tree of all classes in Java. The root of the
tree, the class from which all other classes are
derived is called Object. All classes inherit the
methods of Object.
Terminology
We often use the term subclass for a derived class
and superclass for the base class. Also some use
the terms child class and parent class. We also use
the terms descendant class and ancestor class for
class related by more than one level of inheritance.
Note that deriving a class is very different from
instantiating a class. We are not creating any
objects, but rather, modifying a blueprint.
Overriding Methods
A derived class may override a method inherited
from the base class. We simply write a new
method with the same name and parameter list
(number and types – names don't matter here) as
the method in the base class. When the method is
called upon an object of the derived class, the
derived class' version of the method is invoked.
Overloading Methods
Note that overriding a method is different from
overloading a method. We overload a method
when the parameter list of the new method is
different (different number or types) than the
parameter list of the original method. Java will call
the appropriate method based on the number and
types of the arguments in the call to the method.
It is a common mistake to overload when we me to
override a method.
The Return Type
In general, you cannot change the return type of an
overridden method. The exception is that if the
base method has a return type which is a class, the
derived class may have a return type which is a
descendant of the base class' return type. For
example, if the BankAccount class has a method
that returns a BankAccount, the SavingsAcct class
could override that method and return a
SavingsAcct.
Restricting Access
Some times we only want to allow methods of a
derived class to call a method in the base class. We
can use the access modifier protected (as opposed
to public or private) to do so. That is, if a method
or field is protected, only the methods of the class
and any derived classes may access that field or
method.
Object Class
All classes are descendants of the Object class. The
Object class has no fields but does have several
useful methods, for example, the toString method.
However, these methods work for an generic
object, not your specific object and may not do
what you want. Thus, it's often useful to override
these methods.
toString Method
The toString method is used to return (as a String) a
human-readable description of the object. It should
create a string with the information from the
pertinent fields formatted in an easily
understandable way. The default toString method
prints out the name of the class and a
representation of the pointer to the data object.
equals Method
The equals method is useful for comparing two
objects to check if they are the same. The default
equals method returns true if and only if the two
objects are the same data object in memory. Usual,
you should override this method to return true if
and only if the two objects represent the same
thing, i.e., the relevant fields are equal.
equals Method (cont'd)
Note the definition of the equals method is:
public boolean equals(Object other)
That is, the argument is of type other. To use the
fields and methods of the actual class of objects
being compared, you will need to typecast other to
the right type, i.e., ((Ship)other).size.
Other Useful Stuff
The instanceOf operator checks if an object is of a
particular type. The syntax is:
<Object> instanceof <ClassName>
The getClass() method of class Object returns the
class that was used to create the object.
Abstract Class
An abstract class is a class that can never been
instantiated. It should be used for a base class
when no generic object will ever be created, but
rather, only objects of the derived classes can be
created. For example, in the Nethack program,
there is no such thing as a generic Creature, only
Dragon, UmberHulk, Human, etc. The keyword
abstract is used to denote an abstract class.
Using an Abstract Class
One time we need to use an abstract class is when we
have an array which contains objects of different
class. The abstract base class is the type of the
object stored in the array. For example, in the
Battleship program, the board had a field which
was a 10x10 array of Thing, where a Thing could
be a Ship, Water, Hit, or Miss. Thing would be an
abstract base class.
Abstract Base Class (cont'd)
An abstract base class may have fields and methods
that are inherited by the derived classes.
Abstract Method
On ocassion, all the derived classes have the same
method, but the method is different for each
derived class. That is, there is no one generic
method that is used by any class. However, we
would like to specific that every derived class
should have such a method (we just don't know
what the code is!)
An abstract method in a base class is one that has no
code, but must be overridden in any derived class.