Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Post on 17-Dec-2015

214 views 1 download

Tags:

transcript

Inheritance and Polymorphism

Dr. M.M. van Baal

Verdugo Hills High

What does Inheritance mean in English?

What about Polymorphism?

• Umm…

Before the Java definitions…

• Take out a piece of paper and draw an automobile.

My next automobile.(Anybody draw something like

this?):

My next automobile.(According to my wife):

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

– Make turns

– Weave through traffic

– Attract attention (both of the police and the opposite sex)

• Do 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: public class Automobile– Members (= fields = instance variables):

• public String engine

• public int doors

• public int seats

– Methods (= to “do” things):• startEngine()

• refuel()

• accelerate()

And extend its definition

• We will add and replace what we need– public class SportsCar extends Automobile

• makeTurns()• 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 (= inherit) everything else:– startEngine()– refuel()

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

• 1 Command, implemented differently by different classes (here: different cars).

• Example pumpGas( ) method:– Sports car (at back of car).– Minivan (on right side of car).– Luxury car (on left side of car).– Pickup truck (behind rear license plate).

An auto is an auto

• In other words, whatever kind of Automobile you have, you can pump gas into it.

• Java will ask the Automobile (regardless of what kind of car it is) to pumpGas() and all cars fill gas in their own way e.g. side of the car, rear of the car, etc.

Another polymorphic example.

• Consider the superclass: public class Animals.• Animals has an eat() method.• There are several classes that inherit from Animals:

– public class Bear extends Animals

– public class Chicken extends Animals

– public class Tarantula extends Animals

– public class Cockroach extends Animals

eat() method:

• You can call the eat() method in Animals.• However, every animal eats differently => each animal

defines the eat() method in Animals differently.• How do they eat():

– Bear?– Chicken?– Tarantula?– Cockroach?

• Did you know a cockroach can live for about a week without its head until it finally dies of hunger? Just info: nothing to do with Java!!!!

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