slide 0. inheritance and polymorphism mr. landa south east high
DESCRIPTION
What Does Inheritance Mean in English?TRANSCRIPT
![Page 1: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/1.jpg)
Slide 0
![Page 2: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/2.jpg)
Inheritance and Polymorphism
Mr. LandaSouth East High
![Page 3: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/3.jpg)
What Does Inheritance Mean in English?
![Page 4: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/4.jpg)
What About Polymorphism?
• Umm…
![Page 5: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/5.jpg)
Before the Java definitions…
• Take out a piece of paper and draw an automobile.
![Page 6: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/6.jpg)
Anybody draw something like this?
![Page 7: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/7.jpg)
My next automobile (according to my wife)
![Page 8: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/8.jpg)
They look a little different
• Would you agree they both have…– 4 tires– An engine– Windows– Doors– Seats– Can start their engines– Refuel by putting gas in the tank
• Therefore they must both be part of the same class: Automobile
![Page 9: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/9.jpg)
Therefore…
• Since they are both the same class, they must be able to do the following the same way too– Accelerate– Take tight turns– Weave through traffic– Attract attention (both of police and the opposite sex)
• You agree?
![Page 10: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/10.jpg)
Not quite…
• There are a lot of similarities, both are indeed automobiles
• But one is a sports car and one is a minivan• Are those different classes?
![Page 11: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/11.jpg)
More like different subtypes
• Think about how sports cars were invented– Did they start all over from scratch?– Someone took the automobile and made a more
specific type
![Page 12: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/12.jpg)
How does this work in java?
• We take the class Automobile– Members
• Engine, doors, seats, etc– Methods
• startEngine()• refuel()• accelerate()
![Page 13: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/13.jpg)
And extend it’s definition
• We will add and replace what we need– SportsCar extends Automobile
• takeTightTurns()• weaveThroughTraffic()• attractAttention()• accelerate()
– Notice we’re replacing accelerate() that is defined in Automobile. That’s because the SportsCar needs to expel a very loud purr from the engine as it accelerates.
• We will keep everything else– startEngine(), refuel(), etc
• We get this for free and don’t have to write the code again
![Page 14: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/14.jpg)
Inheritance in terms of Java
• Put it in your own words!
![Page 15: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/15.jpg)
So, Polymorphism?
• I want to work at a full service gas station• To train me, do they need show me how to
pump gas into a:– Sports car– Minivan– Luxury car– Pickup truck
![Page 16: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/16.jpg)
An auto is an auto
• Polymorphism allows us to deal with the “many shapes” of automobiles as just automobiles
• In other words, whatever Automobile shows up, I can pump gas into it.
• Java will ask the Automobile (regardless of what type it is) to refuel() and it will do the right thing.
![Page 17: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/17.jpg)
What about accelerating?
• for refuel() this may seem a little obvious since the method is only defined in Automobile.
• What about methods that were replaced (redefined) like accelerate()?
• If I had 5 Autos lined up and tried to accelerate() them, Java would do the right thing. The Autos that are sportsCars would accelerate with the loud “purr”.
![Page 18: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/18.jpg)
attractAttention()
• If you had a street with 5 Autos and all Autos could attractAttention(), they would do so in the appropriate way for their subclass– SportsCars would have police officers saying “I’m
about to meet my quota”– LuxuryCars would have people saying “I wonder if
he/she is single?”– MiniVans would have people reading the bumper
stickers to find out what school the kids are honors students at
![Page 19: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/19.jpg)
Inheritance in GridWorld
• class Bug extends Actor– Inherits putSelfInGrid()– Replaces act()– Adds canMove()
• class BoxBug extends Bug– Inherits canMove()– Replaces act()
![Page 20: Slide 0. Inheritance and Polymorphism Mr. Landa South East High](https://reader035.vdocument.in/reader035/viewer/2022073115/5a4d1b5d7f8b9ab0599abcad/html5/thumbnails/20.jpg)
Polymorphism in action
• For each Actor (whether Actor, Bug, etc) in the grid, act() is called in each step:
for (Actor a : actors) { // only act if another actor hasn't removed a if (a.getGrid() == gr) a.act(); }