Module 3 Part 2 Polymorphism OBJECTIVES

Published  . 0 views
↓ Download
Module 3 Part 2 Polymorphism OBJECTIVES
1 / 1
Module 3 Part 2 Polymorphism OBJECTIVES - slide 1 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 2 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 3 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 4 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 5 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 6 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 7 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 8 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 9 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 10 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 11 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 12 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 13 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 14 of 15 Module 3 Part 2 Polymorphism OBJECTIVES - slide 15 of 15
Description: Module 3 Part 2 Polymorphism OBJECTIVES Terminology: what is polymorphism? Review of Inheritance and overriding Dynamic Binding Late Binding Pay attention! This is the number one technical interview topic Introduction Polymorphism: Poly

Related Topics

Download Presentation

"Module 3 Part 2 Polymorphism OBJECTIVES" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. Module 3 Part 2
Polymorphism<br>
slide2. OBJECTIVES Terminology: what is polymorphism?

Review of Inheritance and overriding

Dynamic Binding / Late Binding

Pay attention! This is the number one technical interview topic<br>
slide3. Introduction Polymorphism:

Poly – means “many” (πολλές - “polles”)

Morph – “forms” (μορφές – “morfes”)

Objects that take more than one form<br>
slide4. 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.<br>
slide5. 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<br>
slide6. Example: Subclasses override Eat( ) and toString( ) Note: A Dog is-a Mammal. A Cow is-a Mammal.<br>
slide7. 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<br>
slide8. 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<br>
slide9. 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<br>
slide10. 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…<br>
slide11. Fantasy Game: Zombie Kitten Apocalypse It’s my game. I can do what I want.
All subclasses override methods.<br>
slide12. 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();
}<br>
slide13. 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<br>
slide14. 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<br>
slide15. 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.<br>