Your SlideShare is downloading. ×
Object-Oriented Polymorphism Unleashed
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Object-Oriented Polymorphism Unleashed


Published on

Object-Oriented Programming with special emphasis on Polymorphism

Object-Oriented Programming with special emphasis on Polymorphism

Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


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