OO Polymorphism
Unleashed
Naresh Chintalcheru
Lecturer - School of Information Technology at Illinois State University (ISU)
Polymorphism
● Polymorphism is the difficult concept to
understand in Object Oriented
Programming (OOP)
● Nature of support provided by the
Programming Languages make it more
difficult
Programming language support
● Java supports polymorphism with
Inheritance and Interfaces.
● Java also supports restricted
polymorphism with "final" modifier so
that methods cannot be overridden
● C++ supports multiple inheritance and
operator overloading, which Java does
not
OOP
Quick OOP review
before we dig deeper into
Polymorphism
Object Oriented Programming
OOP ....
● Represents concepts as "objects" that have data
fields and methods
● Data fields and actions/methods are closely
associated with object
● Objects are mapped to real world entities
● Collection of interacting objects as opposed to
list of tasks
Object Oriented Programming
OOP ....
● Objects has distinct role and responsibility
● Encourages data hiding
● Objects are capable of receiving messages,
processing data and sending messages to other
objects
● Programing construct that combines data with
set of methods for accessing and modifying data
OOP Concepts
OOP concepts ....
● Encapsulation
● Abstraction
● Inheritance
● Polymorphism
Encapsulation
● Encapsulation
Binds together data and methods, which act on the data.
Enforces modularity and hides complexity by restricting
access to Accessors and Mutators
● Accessor
Accessor is a method which asks an object about itself
(The get and other public methods)
● Mutator
Mutator is a method that modifies the state of an object
(The set and other public methods)
Abstraction and Inheritance
● Abstraction
Presents simple concept or entity to the external world
and hides implementation details
● Inheritance
Allow object data and methods pass down the knowledge
to the hierarchy of inherited objects
Polymorphism
Polymorphism
Polymorphism
Polymorphism
The Word Polymorphism comes from Greek
means "several different forms"
Polymorphism
Polymorphism
Objects can take many
shapes or forms
Polymorphism
Polymorphism Enables
"Programming in General"
rather than
"Programming in Specific"
Polymorphism
Polymorphism Example
Polymorphism
Java supports
Polymorphism with
Inheritance & Interfaces
Polymorphism
Two different object types
ArrayList
&
LinkedList
Polymorphism
In polymorphism you treat both
ArrayList & LinkedList as the
same type
Polymorphism
ArrayList & LinkedList
inherit from
List Interface
Polymorphism
List
|
_________________
| |
ArrayList LinkedList
Polymorphism
List (Generalized)
|
_________________
| |
ArrayList LinkedList
(Specialized)
Polymorphism
Polymorphism treats two types
ArrayList & LinkedList
as one type List
Polymorphism
Polymorphism Employee Self
Service web application example
Employee Types
● Salaried
● Hourly
Polymorphism
List list;
public void addToList(String msg) {
list.add(msg); //Generalized
}
public void setList() {
list = new ArrayList()
//list = new LinkedList()
}
Polymorphism
Polymorphism in java implemented using
Overloading
and
Overriding
and
Parametric
Polymorphism
Overloading
Compile time polymorphism
Overriding
Runtime time polymorphism
Parametric
Runtime time polymorphism
Polymorphism
Overloading
● Two or more methods with same name but
different method signature (differ by number
or type of method parameters) in the same
scope
● Can have different method return types
● Can occur either in the same class or
subclass
Polymorphism
Overriding
● Different implementation of same method in
the inherited classes
● Must have same method name, signature and
return type.
● Appears in subclass
Polymorphism
Overriding
● Access modifiers of the overriding method may not be
more restrictive
○ If the superclass method is public then the overriding method must be public
○ If the superclass method is protected then the overriding method may be
protected or public
○ If the superclass method is default, the overriding method may be default,
protected, or public
○ If the superclass methods is private, it is not inherited and overriding is not
possible
Polymorphism
Parametric
● Using Generic Type (Jdk1.5) which allows
Collections to hold any type of object in
runtime without any change in code
● This is achieved by passing actual Type as
parameter
Polymorphism
Parametric
interface sideCache {
public void put(K key, V value);
public V get(K key);
}
Polymorphism
Life without Polymorphism
Polymorphism
Life without Polymorphism
● Without polymorphism the code quantity and
complexity grows.
● The results of polymorphism can be achieved
using alternative ways
Polymorphism
Alternative Ways to achieve Polymorphism
● instanceof and downcasting
● Java Reflections
Alternative to Polymorphism
class Animal {
}
class Dog extends Animal {
public void woof() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void meow() {
System.out.println("Meow!");
}
}
class Tiger extends Animal {
public void roar() {
System.out.println("Roar!");
}
}
Alternative to Polymorphism
class AlternativePolymorphismExample {
public static void main(String[] args) {
makeItTalk(new Cat());
makeItTalk(new Dog());
makeItTalk(new Hippopotamus());
}
public static void makeItTalk(Animal animal) {
if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.meow();
}
else if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.woof();
}
else if (animal instanceof Tiger) {
Tiger tiger = (Tiger) animal;
tiger();
}
}
}
Polymorphism
Example Using Polymorphism
Using Polymorphism
abstract class Animal {
public abstract void talk();
}
class Dog extends Animal {
public void talk() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void talk() {
System.out.println("Meow!");
}
}
class Tiger extends Animal {
public void talk() {
System.out.println("Roar!");
}
}
Using Polymorphism
class PolymorphismExample {
public static void main(String[] args) {
makeItTalk(new Cat());
makeItTalk(new Dog());
makeItTalk(new Tiger());
}
public static void makeItTalk(Animal animal) {
animal.talk(); //Polymorphism magic
}
}
Polymorphism
Polymorphism Employee Pay Java programs
Employee
SalariedEmp HourlyEmp
Polymorphism
Polymorphism Java Examples
Shape
Circle Rectangle
Cylinder Square
Advantages of Polymorphism
● Code reusability
● Maintainability
● Extensibility
● Reduction in Complexity
Disadvantages of Polymorphism
● Code harder to read
● More design time
● Expert programmers harder to
find

Object-Oriented Polymorphism Unleashed