+ All Categories
Home > Documents > Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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

Date post: 17-Dec-2015
Category:
Upload: muriel-neal
View: 214 times
Download: 1 times
Share this document with a friend
Popular Tags:
20
Transcript
Page 1: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.
Page 2: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Inheritance and Polymorphism

Dr. M.M. van Baal

Verdugo Hills High

Page 3: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

What does Inheritance mean in English?

Page 4: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

What about Polymorphism?

• Umm…

Page 5: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Before the Java definitions…

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

Page 6: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

My next automobile.(Anybody draw something like

this?):

Page 7: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

My next automobile.(According to my wife):

Page 8: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills 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: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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?

Page 10: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills 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: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills 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: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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

Page 13: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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

Page 14: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Inheritance in terms of Java

• Put it in your own words!

Page 15: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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

Page 16: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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.

Page 17: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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

Page 18: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

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!!!!

Page 19: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Inheritance in GridWorld

• class Bug extends Actor– Inherits putSelfInGrid()– Replaces act()– Adds canMove()

• class BoxBug extends Bug– Inherits canMove()– Replaces act()

Page 20: Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills 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(); }


Recommended