200 likes | 208 Views
Learn the concepts of inheritance and polymorphism in Java with practical examples. Understand how classes can inherit properties and behavior from a parent class, and how polymorphism allows objects to be treated as instances of their parent class.
E N D
Inheritance and Polymorphism Mr. Landa South East High
What About Polymorphism? • Umm…
Before the Java definitions… • Take out a piece of paper and draw an automobile.
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
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?
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?
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
How does this work in java? • We take the class Automobile • Members • Engine, doors, seats, etc • Methods • startEngine() • refuel() • accelerate()
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
Inheritance in terms of Java • Put it in your own words!
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
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.
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”.
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
Inheritance in GridWorld • class Bug extends Actor • Inherits putSelfInGrid() • Replaces act() • Adds canMove() • class BoxBug extends Bug • Inherits canMove() • Replaces act()
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(); }