Introduction to Polymorphism and Abstract Classes
Download
Report
Transcript Introduction to Polymorphism and Abstract Classes
Lecture 10 – Polymorphism
Nancy Harris with additional slides
Professor Adams
from Lewis & Bernstein
Inheritance - Preventing a Method
from Being Overridden
The final modifier will prevent the overriding
of a superclass method in a subclass.
public final void message()
If a subclass attempts to override a final
method, the compiler generates an error.
This ensures that a particular superclass
method is used by subclasses rather than a
modified version of it.
Protected Members
Using protected instead of private makes some tasks
easier.
However, any class that is derived from the class, or is in
the same package, has unrestricted access to the
protected member.
It is always better to make all fields private and then
provide public methods for accessing those fields.
If no access specifier for a class member is provided, the
class member is given package access by default.
Any method in the same package may access the
member.
Polymorphism
from the Greek
poly = many
morph = forms
We have seen polymorphism
Think clocks.
You can create a Clock or an AlarmClock.
You can create a Clock and instantiate an
AlarmClock.
The method used (like the updateTime
method) will be based on the object type.
But the object declaration determines
which methods are available to us.
See examples
Polymorphism
A reference can be polymorphic, which can be
defined as "having many forms"
obj.doIt();
This line of code might execute different methods
at different times if the object that obj points to
changes
Polymorphism
Polymorphic references are resolved
at run time; this is called dynamic
binding
Careful use of polymorphic
references can lead to elegant,
robust software designs
Polymorphism can be accomplished
using inheritance or using
interfaces(to be discussed later)
References and Inheritance
An object reference can refer to an object of its
class, or to an object of any class related to it by
inheritance
For example, if the Holiday class is used to
derive a child class called Christmas, then a
Holiday reference could be used to point to a
Christmas object
Holiday
Holiday day;
day = new Christmas();
Christmas
8
References and Inheritance
An Object reference can be used to refer to any
object
An ArrayList is designed to hold Object
references
See API for ArrayList
9
Polymorphism via Inheritance
It is the type of the object being referenced, not
the reference type, that determines which method
is invoked
Suppose the Holiday class has a method called
celebrate, and the Christmas class overrides it
Now consider the following invocation:
day.celebrate();
If day refers to a Holiday object, it invokes the
Holiday version of celebrate; if it refers to a
Christmas object, it invokes the Christmas
version
Polymorphism via Inheritance
Consider the following class
hierarchy:
StaffMember
Volunteer
Employee
Executive
See page 485 for the full UML diagram
Hourly
Polymorphism via Inheritance
Now consider the task of paying all
employees
See
See
See
See
See
See
See
Firm.java
Staff.java
StaffMember.java
Volunteer.java
Employee.java
Executive.java
Hourly.java
Polymorphism in another view
StaffMember you
Volunteer
StaffMember me
Executive
Object Class
The Granddaddy of all classes.
We override the toString and equals
methods commonly in creating our own
classes.
Question – Is it possible for us to write
code that will do something different
based on the class of the object instead of
the class in which the variable is declared.
Like, can we prevent the run time errors
we saw in the Executive vs Hourly
classes.
Class class
Object class has a getClass method
that returns the Class of the object.
The Class class has a getName
method which returns a String
equivalent to the name of the Class.
Using these two, we could write
code that will do one thing for one
class of object and another for a
different class of object.
Meanings of Polymorphism
In this course:
At run-time, a variable may "take the form
of" any of its descendants.
Related Concepts:
Dynamic binding
Polymorphism in Java
Class Membership
The compiler uses the declared class
At run-time an object knows its actual class
Search Order:
Think of a method call as being a “message” to the object.
The message may contain parameters or it may not.
The message is always asking the object to do something.
If a "message" is sent to an object of the derived class then the
derived class is searched (for the existence of such a method)
first and the base class is searched second. (Note: The search
will move up the class hierarchy until found.)
If the "message" is sent to an object of the base class then only
the base class is searched.
At compile time we know which messages are valid; at run time we
use the particular version of the message that corresponds to the
object reacting to the message.
Taking it a step further
Abstract classes – templates for
class families
Rules for Abstract Classes
An abstract class cannot be
instantiated
A class that can have instances is
said to be concrete
An abstract class provides a
prototype for other classes to follow
Subclasses of an Abstract Class
will inherit the variables and
methods of the abstract class
will have the same basic
characteristics
are free to redefine variables and
methods and add new ones
must override any abstract
methods of its parent (unless it
itself is abstract).
Abstract Classes
generally contain at least one abstract
method
are any classes containing at least one
abstract method
can contain non-abstract methods
If there is an abstract method, then the
class must be declared as abstract, but…
If a class is declared as abstract it may or
may not have non-abstract or abstract
methods.
Abstract Methods
have the word abstract in their
declaration
do not have a body
end their declarations with a semi-colon
must be overridden in concrete children
are generally methods whose bodies will
change from one subclass to another
Standard UML Notation
+ public
- private
# protected
{abstract} (name is italicized)
top compartment – class name
middle compartment – class attributes
name : type
bottom compartment – class methods
name (parameterList types) : return type
Abstract Classes
The child of an abstract class must
override the abstract methods of the
parent, or it too will be considered
abstract
An abstract method cannot be defined as
final (because it must be overridden) or
static (because it has no definition yet)
The use of abstract classes is a design
decision – it helps us establish common
elements in a class that is too general to
instantiate
Let’s look again at the StaffMember
class
What will happen if:
We remove the word abstract in the
class header?
We change the abstract method to a
non-abstract method?
We try to instantiate a StaffMember
object?
We decide not to “pay” Volunteers?
We create a new class from the
StaffMember object which is an
abstract class also?
Another example
Shapes - n11/DesigningClasses.pdf
What should the hierarchy look like?
Which classes should be abstract?
Which concrete?
What services should be required
(made abstract)?
n11/ShapeUML.pdf
Finished product
Shape.java
TwoDimensionalShape.java
ThreeDimensionalShape.java
Sphere.java
Rectangle.java