20
Slide 0

Slide 0. Inheritance and Polymorphism Mr. Landa South East High

Embed Size (px)

DESCRIPTION

What Does Inheritance Mean in English?

Citation preview

Page 1: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

Slide 0

Page 2: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

Inheritance and Polymorphism

Mr. LandaSouth East High

Page 3: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

What Does Inheritance Mean in English?

Page 4: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

What About Polymorphism?

• Umm…

Page 5: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

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

Anybody draw something like this?

Page 7: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

My next automobile (according to my wife)

Page 8: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

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

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

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

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

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

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

Inheritance in terms of Java

• Put it in your own words!

Page 15: Slide 0. Inheritance and Polymorphism Mr. Landa South East High

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

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

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

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

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

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(); }