Polymorphism in Java
Polymorphism in Java
SZABIST
Islamabad
Polymorphism
Pre-‐requisites
• Following
are
the
post
condi?ons
that
must
be
met
in
code
in
which
you
want
to
exploit
polymorphism:
– Inheritance
There
should
exist
a
hierarchy
where
some
classes
are
parent
of
the
child
classes
– Overriding
It
is
utmost
important
that
you
should
have
overridden
a
method
in
all
of
these
classes.
– It
is
for
this
method
you
will
observe
polymorphic
behavior.
Polymorphism:
Class
Diagram
Inheritance
Overriding
What
is
leI………..
1-‐-‐-‐-‐-‐-‐>Subsitu?on
Person
p
=
new
Student();
//
child
object
assigned
to
a
parent
type
2-‐-‐-‐-‐-‐-‐>
Method
Calling
p.toString();
//will
s?ll
call
student’s
toString()
Method
output
is
polymorhic
i.e.
even
though
the
object
reference
is
of
person
type,
it
has
the
capability
to
call
toString
of
all
its
child
classes.
Polymorphism 1
§ The is-a relationship between a superclass and a subclass has an
important implication. For example:
§ A cow is an animal
§ Therefore all cows are animals.
§ So you can substitute an object of the subclass Cow for an object of
the superclass Animal, because a cow is, after all, an animal.
However, note that the reverse is not true: not all animals are cows.
Therefore, the following line of code is not valid:
§ Note: The compiler only knows about the declared object reference types.
However, at runtime, the JVM knows what the referred object really is.
§ Note that the capability to convert an object reference from one type (Animal
in our example) to another type (say Cow) is at the heart of polymorphism.
§ This conversion can happen to both kinds of data types: primitives and
object
8
references.
Polymorphism 5
§ Polymorphism does not apply to the static class
members.
12
Conversion of Object Reference Types 4
§ A class type may be converted to another class type if one of the following is true:
§ Another array if the following conditions are true: both <sourceType> and <targetType> are
arrays of object reference types, and the object reference type of <sourceType> array
elements is convertible to the object reference types of <targetType> array elements.
14
Conversion of Object Reference Types 6
§ As an example, consider the following lines of code:
§ This code would work because the class Lab is being converted into
the class ClassRoom, and
Lab is a subclass of ClassRoom.
§ On the other hand, the following code will generate a compiler error:
§ The class LectureHall is being converted into the interface Facilities
§ LectureHall extends ClassRoom
§ ClassRoom implements Facilities.
17
Conversion of Object Reference Types 9
§ On the other hand, line 10 will generate a compiler error because
§ the type of elements of array crooms (that is, ClassRoom) is not
convertible to the type of elements of array labs (that is, Lab).
18
Conversion of Object Reference Types 10
§ An object of any class (and even an array of any type) can
be passed into the single add (Object ob) method.
Conversion of Object Reference Types 13
§ Therefore, at compile time we do not know to which object type this
reference variable csroom refers.
Conversion of Object Reference Types 15
§ For Example:
Lab lab1;
if (cmdLineArg=1){
lab1 = new Lab();
}
else
{
lab1 = new SmallLab(); \\ if we assume a class SmallLab
}
§ Again at compile time we do not know to which object type this
reference variable csroom refers
Conversion of Object Reference Types 16
§ When both the source type and target type are classes, one
class must be a subclass of the other.
§ When both the source type and target type are arrays, the
elements of both the arrays must be object reference types
and not primitive types. Also, the object reference type of the
source array must be convertible to the object reference type
of the target array.
1. Auditorium a1,a2;
2. ClassRoom c;
3. LectureHall lh;
4. a1 = new Auditorium();
5. c = a1; //legal implicit conversion.
6. a2 = (Auditorium) c; // Legal cast.
7. lh = (LectureHall) c; // Illegal
Conversion of Object Reference Types 19
§ Note that the code fragment has four object references, namely a1,
a2, c, and lh.
§ In line 6, the compiler cannot determine the class of the object to
which c refers.
§ In line 7, the compiler cannot determine the class of the object to which c
refers.
§ However, at execution time, the JVM knows that c refers to an object of class
Auditorium (due to line 5).
§ Thus, the conversion in line 7 is illegal, and will generate an exception at
runtime.
§ For this to be legal, the class of the object to which c refers has to be either
LectureHall or a subclass of it.
§ When an attempt to cast a reference variable to a type is illegal, the JVM
throws the
ClassCastException