Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Slide 0. Inheritance and Polymorphism Mr. Landa South East High."— Presentation transcript:

1 Slide 0

2 Inheritance and Polymorphism Mr. Landa South East High

3 What Does Inheritance Mean in English?

4 What About Polymorphism? Umm…

5 Before the Java definitions… Take out a piece of paper and draw an automobile.

6 Anybody draw something like this?

7 My next automobile (according to my wife)

8 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

9 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?

10 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?

11 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

12 How does this work in java? We take the class Automobile –Members Engine, doors, seats, etc –Methods startEngine() refuel() accelerate()

13 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

14 Inheritance in terms of Java Put it in your own words!

15 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

16 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.

17 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”.

18 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

19 Inheritance in GridWorld class Bug extends Actor –Inherits putSelfInGrid() –Replaces act() –Adds canMove() class BoxBug extends Bug –Inherits canMove() –Replaces act()

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


Download ppt "Slide 0. Inheritance and Polymorphism Mr. Landa South East High."

Similar presentations


Ads by Google