Understanding Polymorphism in Object-Oriented Programming

module 3 n.w
1 / 15
Embed
Share

Learn about polymorphism in object-oriented programming through real-world analogies and code examples. Explore the concepts of inheritance, overriding, dynamic binding, and late binding in this informative guide.

  • Polymorphism
  • OOP
  • Inheritance
  • Binding
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Module 3 Part 2 Polymorphism

  2. OBJECTIVES Terminology: what is polymorphism? Review of Inheritance and overriding Dynamic Binding / Late Binding Pay attention! This is the number one technical interview topic

  3. Introduction Polymorphism: Poly means many ( - polles ) Morph forms ( morfes ) Objects that take more than one form

  4. You are a student In this class you are a student, you may expose methods such as study(), take_test(). But when you are home, you are son/daughter/parent/sibling/partner etc. In that context, you may expose do_dishes(), clean_room(), care_for_family() When you go to work you are an employee, in that context you may empty_box(), work_cashregister(). You are still the same person, but you ACT as different people depending on your contex. This is polymorphism in the real world. You are different depending on your contex.

  5. A quick Recap: Inheritance Before you can understand Polymorphism, you must understand Inheritance By the end of this lecture, you must understand how these concepts are different from one another You must also understand how they are related Review: A subclass can override (redefine) a method that it inherits

  6. Example: Subclasses override Eat( ) and toString( ) Mammal Output + Eat( ) + toString () A mammal eats food Cow Dog Output Output A cow eats grass + Eat( ) + toString () + Eat( ) + toString () A dog eats bones Note: A Dog is-a Mammal. A Cow is-a Mammal.

  7. Polymorphism pay attention! Mammal m = new Mammal(); // weird, but m is a Mammal m = new Dog(); // reassign m to be a Dog m.Eat(); // BOOM! Polymorphism right here! // m is a Dog and eats like a Dog m = new Cow(); // now m is a Cow! m.Eat(); // BOOM! More polymorphism! // m is a Cow and eats like a Cow This example is the easiest way to understand polymorphism m is a Mammal, but references an instance of a child class (Dog or Cow) m changes type during runtime and behaves differently

  8. Polymorphism Late binding Normally a method is bound to an object at compile time This means the compiler knows which methods to run at compile time This is called early binding When methods are bound when the program is running This is how polymorphism works We don t know what m is until the program is running so we have to determine which method to call at the last minute This is called late binding Employers *love* asking questions about early and late binding

  9. Before the next example Ask yourself: What is inheritance? What is polymorphism? How are inheritance and polymorphism related? How are they different? You need to be able to clearly articulate these to an employer

  10. Last (and very real) example (your welcome, CGDD students ) Imagine you have to design a video game There s a player and three (3) different enemies You also have projectiles and other stuff All gaming things need to update( ) themselves Position things need to move (just a bit) on the screen between frames Hit points/health Other stats Artificial Intelligence needs to run How would you design this? Let s focus on just the enemies

  11. Fantasy Game: Zombie Kitten Apocalypse Enemy - location - health - player location + Move( ) + UpdateAI( ) + DrawYourself( ) Zombie Chihuahua Kitten + Move( ) + UpdateAI( ) + DrawYourself( ) + Move( ) + UpdateAI( ) + DrawYourself( ) + Move( ) + UpdateAI( ) + DrawYourself( ) It s my game. I can do what I want. All subclasses override methods.

  12. The Power of Polymorphism Imagine now that we create an array of 100 Enemies: Enemy[ ] enemies = new Enemy[100]; Fill it with random enemies: enemies[0] = new Kitten(); enemies[1] = new Zombie(); enemies[2] = new Chihuahua(); // and so on BOOM Polymorphism! Tell ALL enemies to do their thing for (int i = 0; i < enemies.size; i++) { enemies[i].updateAI(); enemies[i].move(); enemies[i].drawYourself(); }

  13. Type matching When assigning values, the data type on the right side must match the data type on the left side: int i = 17; // types match, so we re good int i = a ; // type mismatch

  14. Ploymorphism Are these assignments legal? Enemy e = new Enemy( ); //Yes Zombie z = new Zombie( ); //Yes Chihuahua c = new Chihuahua(); //Yes e = new Chihuahua(); //Yes e = new Zombie(); //Yes z = new Enemy(); //No! The last 2 statements demonstrate polymorphism, where a variable (m) is changing types while the program is running

  15. Is it valid? The way you can tell if an assignment is valid: Look at the object type being instantiated on the right side of the statement. If that is equal or a child of the type on the left side, it s valid. Otherwise it s not. Enemy e = new Chihuahua(); This is valid because Chihuahua is-a Enemy Chihuahua c = new Enemy(); This is not valid because Enemy is-a Chihuahua is not true.

More Related Content