2. 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
3. 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
5. 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
6. 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
8. 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)
9. 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
23. Polymorphism
List list;
public void addToList(String msg) {
list.add(msg); //Generalized
}
public void setList() {
list = new ArrayList()
//list = new LinkedList()
}
26. 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
28. 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
29. 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
32. Polymorphism
Life without Polymorphism
● Without polymorphism the code quantity and
complexity grows.
● The results of polymorphism can be achieved
using alternative ways
34. 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!");
}
}
35. 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();
}
}
}
37. 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!");
}
}
38. 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
}
}