Inheritance and Polymorphism
description
Transcript of Inheritance and Polymorphism
![Page 1: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/1.jpg)
1
Inheritance and Polymorphism
![Page 2: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/2.jpg)
2
This section is not required material!!!! A note about inheritance…
It’s not normally covered in 101 It will be gone over in more detail in CS 201
Ask questions if you are confused about inheritance You aren’t the only one!
![Page 3: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/3.jpg)
3
Motivation Consider a transportation computer game
Different types of vehicles: Planes
Jets, helicopters, space shuttle Automobiles
Cars, trucks, motorcycles Trains
Diesel, electric, monorail Ships
…
Let’s assume a class is written for each type of vehicle
![Page 4: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/4.jpg)
4
More on classes vs. objects
![Page 5: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/5.jpg)
5
Motivation Sample code for the types of planes:
fly() takeOff() land() setAltitude() setPitch()
Note that a lot of this code is common to all types of planes They have a lot in common! It would be a waste to have to write separate fly()
methods for each plane type What if you then have to change one – you would then
have to change dozens of methods
![Page 6: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/6.jpg)
6
Motivation Indeed, all vehicles will have similar methods:
move() getLocation() setSpeed() isBroken()
Again, a lot of this code is common to all types of vehicles It would be a waste to have to write separate move() methods
for each vehicle type What if you then have to change one – you would then have
to change dozens of methods
What we want is a means to specify one move() method, and have each vehicle type inherit that code Then, if we have to change it, we only have to change one copy
![Page 7: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/7.jpg)
7
Motivation
Trains
Automobiles
Planes
Jet
Helicopter
Electric
Monorail
Diesel
Space shuttle
Car
Truck
Motorcycle
Vehicle
Provides:Provides:move()move()
getLocation()getLocation()setSpeed()setSpeed()isBroken()isBroken()
Provides:fly()
takeOff()land()
setAltitude()setPitch()
Provides:derail()
getStation()
Provides:oilChange()isInTraffic()
![Page 8: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/8.jpg)
8
Motivation What we will do is create a “parent” class and a “child” class
The “child” class (or subclass) will inherit the methods (etc.) from the “parent” class (or superclass)
Note that some classes (such as Train) are both subclasses and superclasses
![Page 9: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/9.jpg)
9
Inheritance code
class Vehicle { ...}
class Train extends Vehicles { ...}
class Monorail extends Train { ...}
![Page 10: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/10.jpg)
10
About extends If class A extends class B
Then class A is the subclass of B Class B is the superclass of class A A “is a” B A has (almost) all the methods and variables that B has
If class Train extends class Vehicle Then class Train is the subclass of Vehicle Class Vehicle is the superclass of class Train Train “is a” Vehicle Train has (almost) all the methods and variables that
Vehicle has
![Page 11: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/11.jpg)
11
Object-oriented terminology In object-oriented programming languages, a class created by
extending another class is called a subclass The class used for the basis is called the superclass Alternative terminology
The superclass is also referred to as the base class The subclass is also referred to as the derived class
Monorail Train Vehicle
![Page 12: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/12.jpg)
12
Another example Consider shapes in a graphics program
Shape class Circle class Cube class Dodecahedron class
![Page 13: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/13.jpg)
13
Inheritance Organizes objects in a top-down fashion from most general to
least general
Inheritance defines a “is-a” relationship A mountain bike “is a” kind of bicycle A SUV “is a” kind of automobile A border collie “is a” kind of dog A laptop “is a” kind of computer
![Page 14: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/14.jpg)
14
Packages Allow definitions to be collected together into a single entity—
a package
The classes in our game could be added to a package
Classes and names in the same package are stored in the same folder
Classes in a package go into their own “namespace” and therefore the names in a particular package do not conflict with other names in other packages
For example, a package called OtherGame might have a different definition of Map
![Page 15: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/15.jpg)
15
Controlling access Class access rights
Member Restriction
this Subclass Package General
public
protected
default
private
![Page 16: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/16.jpg)
16
Java’s Mother-of-all-objects—Class Object
Obj ectShapeCi r cl e
Class representing
a circle
Superclass of all J ava objects.
Class representing
any shape
![Page 17: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/17.jpg)
17
Thus, everything extends Object Either directly or indirectly
So what does that give us?
Object contains the following methods: clone() equals() toString() and others…
Thus, every class has those methods
![Page 18: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/18.jpg)
18
A note about equals() Why does the equals() method always have to have the
following prototype: boolean equals(Object obj)
Many other class in the Java SDK require the user of equals() Such as the Vector class
Those classes need to know how the equals() method will work in order for them to work properly Thus, it must have the same prototype
![Page 19: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/19.jpg)
19
Overriding Consider the following code:
class Foo { // automatically extends Objectpublic String toString () {
return “Foo”;}
}...Foo f = new Foo();System.out.println (f);
Now there are two toString() method defined One inherited from class Object One defined in class Foo
And they both have the same prototype!
Which one does Java call?
![Page 20: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/20.jpg)
20
Overriding Java will call the most specific overriden method it can
toString() in Foo is more specific than toString() in Object
Consider our transportation hierarchy: Assume each class has its own toString() method Car extends Automobile extends Vehicle (extends Object) Assume each defines a toString() methods
The toString() method in Vehicle is more specific (to vehicles) than the one in Object
The toString() method in Automobiles is more specific than the ones in Vehicle or Object
The toString() method in Car is more specific than the ones in Automobile, Vehicle, or Object
Thus, for a Car object, the Car toString() will be called There are ways to call the other toString() methods
This has to be specifically requested
![Page 21: Inheritance and Polymorphism](https://reader035.fdocuments.us/reader035/viewer/2022062723/56813baa550346895da4dbd8/html5/thumbnails/21.jpg)
21
Overriding This is called overriding, because the toString() in Foo
“overrides” the toString() in Object
Note that the prototype must be EXACTLY the same With overloading, the parameter list must be DIFFERENT
Overriding only works with inheritance In particular, you can only override a method already
defined in a parent (or grandparent, etc.) class