Download presentation
Presentation is loading. Please wait.
2
Inheritance and Polymorphism
Dr. M.M. van Baal Verdugo Hills High
3
What does Inheritance mean in English?
Ask students to give their answers.
4
What about Polymorphism?
Umm… Break down into poly and morph.
5
Before the Java definitions…
Take out a piece of paper and draw an automobile.
6
My next automobile. (Anybody draw something like this?):
7
My next automobile. (According to my girlfriend):
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 Student’s drawings probably also have the tires, doors, etc.
9
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?
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: public class Automobile Members (= fields = instance variables): public String engine public int doors public int seats Methods (= to “do” things): startEngine() refuel() accelerate()
13
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
14
Inheritance in terms of Java
Put it in your own words!
15
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).
16
An auto is an auto 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 pumpGas() and it will do the right thing.
17
Another polymorphic example.
Consider the class: 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
18
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!!!!
19
Inheritance in GridWorld
class Bug extends Actor Inherits pustSelfInGrid() Replaces act() Adds canMove() class BoxBug extends Bug Inherits canMove()
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.