OO Polymorphism
Unleashed
Naresh Chintalcheru
Lecturer - School of Information Technology at Illinois State University (IS...
Polymorphism
● Polymorphism is the difficult concept to
understand in Object Oriented
Programming (OOP)
● Nature of suppor...
Programming language support
● Java supports polymorphism with
Inheritance and Interfaces.
● Java also supports restricted...
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 an...
Object Oriented Programming
OOP ....
● Objects has distinct role and responsibility
● Encourages data hiding
● Objects are...
OOP Concepts
OOP concepts ....
● Encapsulation
● Abstraction
● Inheritance
● Polymorphism
Encapsulation
● Encapsulation
Binds together data and methods, which act on the data.
Enforces modularity and hides comple...
Abstraction and Inheritance
● Abstraction
Presents simple concept or entity to the external world
and hides implementation...
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 = ...
Polymorphism
Polymorphism in java implemented using
Overloading
and
Overriding
and
Parametric
Polymorphism
Overloading
Compile time polymorphism
Overriding
Runtime time polymorphism
Parametric
Runtime time polymorphi...
Polymorphism
Overloading
● Two or more methods with same name but
different method signature (differ by number
or type of ...
Polymorphism
Overriding
● Different implementation of same method in
the inherited classes
● Must have same method name, s...
Polymorphism
Overriding
● Access modifiers of the overriding method may not be
more restrictive
○ If the superclass method...
Polymorphism
Parametric
● Using Generic Type (Jdk1.5) which allows
Collections to hold any type of object in
runtime witho...
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 pol...
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!");
...
Alternative to Polymorphism
class AlternativePolymorphismExample {
public static void main(String[] args) {
makeItTalk(new...
Polymorphism
Example Using Polymorphism
Using Polymorphism
abstract class Animal {
public abstract void talk();
}
class Dog extends Animal {
public void talk() {
...
Using Polymorphism
class PolymorphismExample {
public static void main(String[] args) {
makeItTalk(new Cat());
makeItTalk(...
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
Upcoming SlideShare
Loading in...5
×

Object-Oriented Polymorphism Unleashed

3,003

Published on

Object-Oriented Programming with special emphasis on Polymorphism

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,003
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
1
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Object-Oriented Polymorphism Unleashed"

  1. 1. OO Polymorphism Unleashed Naresh Chintalcheru Lecturer - School of Information Technology at Illinois State University (ISU)
  2. 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. 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
  4. 4. OOP Quick OOP review before we dig deeper into Polymorphism
  5. 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. 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
  7. 7. OOP Concepts OOP concepts .... ● Encapsulation ● Abstraction ● Inheritance ● Polymorphism
  8. 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. 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
  10. 10. Polymorphism Polymorphism
  11. 11. Polymorphism Polymorphism The Word Polymorphism comes from Greek means "several different forms"
  12. 12. Polymorphism Polymorphism Objects can take many shapes or forms
  13. 13. Polymorphism Polymorphism Enables "Programming in General" rather than "Programming in Specific"
  14. 14. Polymorphism Polymorphism Example
  15. 15. Polymorphism Java supports Polymorphism with Inheritance & Interfaces
  16. 16. Polymorphism Two different object types ArrayList & LinkedList
  17. 17. Polymorphism In polymorphism you treat both ArrayList & LinkedList as the same type
  18. 18. Polymorphism ArrayList & LinkedList inherit from List Interface
  19. 19. Polymorphism List | _________________ | | ArrayList LinkedList
  20. 20. Polymorphism List (Generalized) | _________________ | | ArrayList LinkedList (Specialized)
  21. 21. Polymorphism Polymorphism treats two types ArrayList & LinkedList as one type List
  22. 22. Polymorphism Polymorphism Employee Self Service web application example Employee Types ● Salaried ● Hourly
  23. 23. Polymorphism List list; public void addToList(String msg) { list.add(msg); //Generalized } public void setList() { list = new ArrayList() //list = new LinkedList() }
  24. 24. Polymorphism Polymorphism in java implemented using Overloading and Overriding and Parametric
  25. 25. Polymorphism Overloading Compile time polymorphism Overriding Runtime time polymorphism Parametric Runtime time polymorphism
  26. 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
  27. 27. Polymorphism Overriding ● Different implementation of same method in the inherited classes ● Must have same method name, signature and return type. ● Appears in subclass
  28. 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. 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
  30. 30. Polymorphism Parametric interface sideCache { public void put(K key, V value); public V get(K key); }
  31. 31. Polymorphism Life without Polymorphism
  32. 32. Polymorphism Life without Polymorphism ● Without polymorphism the code quantity and complexity grows. ● The results of polymorphism can be achieved using alternative ways
  33. 33. Polymorphism Alternative Ways to achieve Polymorphism ● instanceof and downcasting ● Java Reflections
  34. 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. 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(); } } }
  36. 36. Polymorphism Example Using Polymorphism
  37. 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. 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 } }
  39. 39. Polymorphism Polymorphism Employee Pay Java programs Employee SalariedEmp HourlyEmp
  40. 40. Polymorphism Polymorphism Java Examples Shape Circle Rectangle Cylinder Square
  41. 41. Advantages of Polymorphism ● Code reusability ● Maintainability ● Extensibility ● Reduction in Complexity
  42. 42. Disadvantages of Polymorphism ● Code harder to read ● More design time ● Expert programmers harder to find

×