University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner Inheritance II Lecture 34, Mon Apr 12 2010 borrowing from slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/111-10 1
Reading ■ This week reading: 2.11-2.12, 9.5-9.8,10.9-10.10 ■ 5.1-5.2, 11.5, 12.2-12.3 (2nd edition) ■ we will not get through all this material in lecture ■ minimal/no coverage on final ■ weekly reading question still due last class Wed 4/14 2
Recap: Parameter Passing Pictures object as parameter primitive as parameter ■ ■ ■ copy of pointer made ■ copy of value ■ pass by reference ■ pass by value ■ modifications visible outside ■ modifications not visible method outside method main main 4 foo number 4 method1 method1 4 x x 3
Recap: Static Fields/Methods ■ Static fields belong to whole class ■ nonstatic fields belong to instantiated object ■ Static methods can only use static fields ■ nonstatic methods can use either nonstatic or static fields object: Giraffe2 class: Giraffe neckLength numGiraffes object: Giraffe1 neckLength sayHowTall() getGiraffeCount() sayHowTall() 4
Recap: Variable Types and Scope Static variables ■ ■ declared within class ■ associated with class, not instance Instance variables ■ ■ declared within class ■ associated with instance ■ accessible throughout object, lifetime of object Local variables ■ ■ declared within method ■ accessible throughout method, lifetime of method Parameters ■ ■ declared in parameter list of method ■ accessible throughout method, lifetime of method 5
Recap: Inheritance ■ Inheritance: process by which new class is derived from existing one ■ fundamental principle of object-oriented programming ■ Create new child class (subclass) that extends existing parent one (superclass) ■ inherits all methods and variables ■ except constructor ■ can just add new variables and methods 6
Recap: Inheritance and Constructors public class CokeMachine2000 extends CokeMachine2 { public CokeMachine2000() { super(); } public CokeMachine2000(int n) { super(n); } public void loadCoke(int n) { numberOfCans = numberOfCans + n; System.out.println("Adding " + n + " cans to this machine"); } } ■ Subclass (child class) inherits all methods except constructor methods from superclass (parent class) ■ Using reserved word super in subclass constructor tells Java to call appropriate constructor method of superclass 7
Recap: Inheritance and Scope ■ Subclasses inherits but cannot directly access private fields or variables of superclass ■ Protected variables can be directly accessed from declaring class and any classes derived from it 8
Some Coke Machine History early Coke Machine • mechanical • sealed unit, must be reloaded at factory • no protection against vandalism 9
Some Coke Machine History Coke Machine 2000 • electro-mechanical • can be reloaded on site • little protection against vandalism 10
Some Coke Machine History Coke Machine UA* • prototype cyberhuman intelligent mobile autonomous vending machine • can reload itself in transit • vandalism? bring it on * Urban Assault 11
Some Coke Machine History Coke Machine UA Assuming that previous generation CokeMachine simulations have wimpy vandalize() methods built-in to model their gutless behavior when faced with a crowbar-wielding human, how do we create the UA class with true vandal deterrence? 12
Method Overriding ■ If child class defines method with same name and signature as method in parent class ■ say child's version overrides parent's version in favor of its own 13
Method Overriding public class CokeMachine2 { private static int totalMachines = 0; protected int numberOfCans; public CokeMachine2() { numberOfCans = 10; System.out.println("Adding another machine to your empire with " + numberOfCans + " cans of Coke"); totalMachines++; } public CokeMachine2(int n) { numberOfCans = n; System.out.println("Adding another machine to your empire with " + numberOfCans + " cans of Coke"); totalMachines++; } public static int getTotalMachines() { return totalMachines; } 14
Method Overriding public int getNumberOfCans() { return numberOfCans; } public void buyCoke() { if (numberOfCans > 0) { numberOfCans = numberOfCans - 1; System.out.println("Have a Coke"); System.out.print(numberOfCans); System.out.println(" cans remaining"); } else { System.out.println("Sold Out"); } } public void vandalize() { System.out.println("Please don't hurt me...take all my money"); } } 15
Method Overriding public class CokeMachine2000 extends CokeMachine2 { public CokeMachine2000() { super(); } public CokeMachine2000(int n) { super(n); } public void loadCoke(int n) { numberOfCans = numberOfCans + n; System.out.println("loading " + n + " cans"); } public void vandalize() // this overrides the vandalize method from parent { System.out.println("Stop it! Never mind, here's my money"); } } 16
Method Overriding public class CokeMachineUA extends CokeMachine2000 { public CokeMachineUA() { super(); } public CokeMachineUA(int n) { super(n); } public void vandalize() // this overrides the vandalize method from parent { System.out.println("Eat lead and die, you slimy Pepsi drinker!!"); } } 17
Method Overriding public class SimVend { public static void main (String[] args) { CokeMachine2[] mymachines = new CokeMachine2[5]; mymachines[0] = new CokeMachine2(); mymachines[1] = new CokeMachine2000(); mymachines[2] = new CokeMachineUA(); for (int i = 0; i < mymachines.length; i++) { if (mymachines[i] != null) { mymachines[i].vandalize(); } } } } > java SimVend Adding another machine to your empire with 10 cans of Coke Adding another machine to your empire with 10 cans of Coke Adding another machine to your empire with 10 cans of Coke Please don't hurt me...take all my money Stop it! Never mind, here's my money. 18 Eat lead and die, you slimy Pepsi drinker!!
Method Overriding ■ If child class defines method with same name and signature as method in parent class ■ say child's version overrides parent's version in favor of its own ■ reminder: signature is number, type, and order of parameters ■ Writing our own toString() method for class overrides existing, inherited toString() method ■ Where was it inherited from? 19
Method Overriding ■ Where was it inherited from? ■ All classes that aren't explicitly extended from a named class are by default extended from Object class ■ Object class includes a toString() method ■ so... class header public class myClass ■ is actually same as public class myClass extends Object 20
Overriding Variables ■ You can, but you shouldn't 21
Overriding Variables ■ You can, but you shouldn't ■ Possible for child class to declare variable with same name as variable inherited from parent class ■ one in child class is called shadow variable ■ confuses everyone! ■ Child class already can gain access to inherited variable with same name ■ there's no good reason to declare new variable with the same name 22
Another View of Polymorphism ■ From Just Java 2 by Peter van der Linden: ■ Polymorphism is a complicated name for a straightforward concept. It merely means using the same one name to refer to different methods. "Name reuse" would be a better term. ■ Polymorphism made possible in Java through method overloading and method overriding ■ remember method overloading? 23
Method Overloading and Overriding ■ Method overloading: "easy" polymorphism ■ in any class can use same name for several different (but hopefully related) methods ■ methods must have different signatures so that compiler can tell which one is intended ■ Method overriding: "complicated“ polymorphism ■ subclass has method with same signature as a method in the superclass ■ method in derived class overrides method in superclass ■ resolved at execution time, not compilation time ■ some call it true polymorphism 24
Objectives ■ Understanding when and how to use abstract classes ■ Understanding tradeoffs between interfaces and inheritance 25
A New Wrinkle ■ Expand vending machine empire to include French fry machines ■ is a French fry machine a subclass of Coke Machine? 26
If We Have This Class Hierarchy... Coke Machine is-a Coke Machine2000 is-a Coke MachineUA 27
...Does This Make Sense? Coke Machine is-a is-a French Fry Coke Machine Machine2000 is-a Coke MachineUA 28
Does This Make More Sense? Generic Vending Machine is-a is-a French Fry Coke Machine Machine is-a Coke Machine2000 is-a Coke MachineUA 29
Does This Make More Sense? Generic Vending Machine is-a is-a French Fry Coke Machine Machine is-a ■ Yes Coke Machine2000 ■ especially if we're thinking of adding all kinds of vending machines... is-a Coke MachineUA 30
Recommend
More recommend