Inheritance and Polymorphism Chapter 11 Spring 2007 CS

  • Slides: 23
Download presentation
Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield 1

Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield 1

This section is not required material!!!! o o A note about inheritance… n It’s

This section is not required material!!!! o o A note about inheritance… n It’s not normally covered in 101 n It will be gone over in more detail in CS 201 Ask questions if you are confused about inheritance n You aren’t the only one! 2

Motivation o o Consider a transportation computer game n Different types of vehicles: o

Motivation o o Consider a transportation computer game n Different types of vehicles: o Planes n Jets, helicopters, space shuttle o Automobiles n Cars, trucks, motorcycles o Trains n Diesel, electric, monorail o Ships n … Let’s assume a class is written for each type of vehicle 3

More on classes vs. objects 4

More on classes vs. objects 4

Motivation o o Sample code for the types of planes: n fly() n take.

Motivation o o Sample code for the types of planes: n fly() n take. Off() n land() n set. Altitude() n set. Pitch() Note that a lot of this code is common to all types of planes n They have a lot in common! n It would be a waste to have to write separate fly() methods for each plane type o What if you then have to change one – you would then have to change dozens of methods 5

Motivation o o o Indeed, all vehicles will have similar methods: n move() n

Motivation o o o Indeed, all vehicles will have similar methods: n move() n get. Location() n set. Speed() n is. Broken() Again, a lot of this code is common to all types of vehicles n It would be a waste to have to write separate move() methods for each vehicle type o 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 n Then, if we have to change it, we only have to change one copy 6

Motivation Provides: fly() take. Off() land() set. Altitude() set. Pitch() Provides: derail() get. Station()

Motivation Provides: fly() take. Off() land() set. Altitude() set. Pitch() Provides: derail() get. Station() Provides: move() get. Location() set. Speed() is. Broken() Provides: oil. Change() 7 is. In. Traffic()

Motivation o o o What we will do is create a “parent” class and

Motivation o o o 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 8

Inheritance code class Vehicle {. . . } class Train extends Vehicles {. .

Inheritance code class Vehicle {. . . } class Train extends Vehicles {. . . } class Monorail extends Train {. . . } 9

About extends o o If class A extends class B n Then class A

About extends o o If class A extends class B n Then class A is the subclass of B n Class B is the superclass of class A n A “is a” B n A has (almost) all the methods and variables that B has If class Train extends class Vehicle n Then class Train is the subclass of Vehicle n Class Vehicle is the superclass of class Train n Train “is a” Vehicle n Train has (almost) all the methods and variables that Vehicle has 10

Object-oriented terminology o o o In object-oriented programming languages, a class created by extending

Object-oriented terminology o o o 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 n The superclass is also referred to as the base class n The subclass is also referred to as the derived class Monorail Train Vehicle 11

10 dimensions n Disclaimer: it doesn’t have much scientific validity n http: //tenthdimension. com/

10 dimensions n Disclaimer: it doesn’t have much scientific validity n http: //tenthdimension. com/ 12

Another example o Consider shapes in a graphics program n Shape class o Circle

Another example o Consider shapes in a graphics program n Shape class o Circle class o Cube class o Dodecahedron class 14

Inheritance o o Organizes objects in a top-down fashion from most general to least

Inheritance o o Organizes objects in a top-down fashion from most general to least general Inheritance defines a “is-a” relationship n A mountain bike “is a” kind of bicycle n A SUV “is a” kind of automobile n A border collie “is a” kind of dog n A laptop “is a” kind of computer 15

Packages o o Allow definitions to be collected together into a single entity— a

Packages o o 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 n For example, a package called Other. Game might have a different definition of Map 16

Controlling access o Class access rights Member Restriction this Subclass Package General public ü

Controlling access o Class access rights Member Restriction this Subclass Package General public ü ü protected ü ü ¾ ¾ default ü ü ü ¾ private ü ¾ ¾ ¾ 17

Java’s Mother-of-all-objects—Class Object 18

Java’s Mother-of-all-objects—Class Object 18

Thus, everything extends Object o Either directly or indirectly o So what does that

Thus, everything extends Object o Either directly or indirectly o So what does that give us? o o Object contains the following methods: n clone() n equals() n to. String() n and others… Thus, every class has those methods 19

More about clone() o o o It’s protected in class Object n Which means

More about clone() o o o It’s protected in class Object n Which means that a Circle class also has a protected version n Recall that protected means that code outside the Circle class can NOT call it If we wanted a clone() method, we would declare it as such: n public Object clone() { … } Otherwise, we would get the following error message: n Method clone() has protected visibility in class Cirlce 20

A note about equals() o o Why does the equals() method always have to

A note about equals() o o Why does the equals() method always have to have the following prototype: n boolean equals(Object obj) Many other class in the Java SDK require the use of equals() n Such as the Vector class Those classes need to know how the equals() method will work in order for them to work properly n Thus, it must have the same prototype Same for to. String() n This method allows Java to “print” out an object 21

Overriding o Consider the following code: class Foo { // automatically extends Object public

Overriding o Consider the following code: class Foo { // automatically extends Object public String to. String () { return “Foo”; } }. . . Foo f = new Foo(); System. out. println (f); o Now there are two to. String() method defined n One inherited from class Object n One defined in class Foo And they both have the same prototype! o Which one does Java call? o 22

Overriding o o o Java will call the most specific overriden method it can

Overriding o o o Java will call the most specific overriden method it can n to. String() in Foo is more specific than to. String() in Object Consider our transportation hierarchy: n Assume each class has its own to. String() method n Car extends Automobile extends Vehicle (extends Object) n Assume each defines a to. String() methods The to. String() method in Vehicle is more specific (to vehicles) than the one in Object The to. String() method in Automobiles is more specific than the ones in Vehicle or Object The to. String() method in Car is more specific than the ones in Automobile, Vehicle, or Object Thus, for a Car object, the Car to. String() will be called n There are ways to call the other to. String() methods o This has to be specifically requested 23

Overriding o o o This is called overriding, because the to. String() in Foo

Overriding o o o This is called overriding, because the to. String() in Foo “overrides” the to. String() in Object Note that the prototype must be EXACTLY the same n With overloading, the parameter list must be DIFFERENT Overriding only works with inheritance n In particular, you can only override a method already defined in a parent (or grandparent, etc. ) class 24