news
play

News CPSC 111, Intro to Computation Jan-Apr 2006 Check your lab 7 - PDF document

University of British Columbia News CPSC 111, Intro to Computation Jan-Apr 2006 Check your lab 7 grade we havent yet handed out midterm solution, but the Tamara Munzner window will close soon! 5/70 midterm points is 1% of your


  1. University of British Columbia News CPSC 111, Intro to Computation Jan-Apr 2006 � Check your lab 7 grade � we haven’t yet handed out midterm solution, but the Tamara Munzner window will close soon! � 5/70 midterm points is 1% of your course grade! � Yet a few more (but not all) Assignment 2s to hand Inheritance II back after class � Assignment 3 due Friday Apr 7, 5pm Lecture 23, Thu Mar 30 2006 � start now now now! � Final exam: Mon Apr 24, 3:30pm, HEBB TH based on slides by Kurt Eiselt � Evaluations today (beginning of class) http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr 1 2 Recap: Comparable Recap: Multiple Interfaces � sort method that works on array of objects of � Classes can implement more than one any type that implements Comparable interface at once � type guaranteed to have compareTo method � contract to implement all abstract methods defined in every interface it implements � sorted � int public class MyClass implements Interface1, Interface2, � String Interface3 � Bunny { } � revisit Bunny.compareTo: checking dynamic type of object 3 4 Recap: Inheritance Recap: Inheritance and Constructors public class CokeMachine2000 extends CokeMachine2 � Inheritance: process by which new class is { public CokeMachine2000() { derived from existing one super(); } � fundamental principle of object-oriented public CokeMachine2000(int n) { super(n); } programming public void loadCoke(int n) { � Create new child class (subclass) that numberOfCans = numberOfCans + n; System.out.println("Adding " + n + " cans to this machine"); } extends existing parent one (superclass) } � inherits all methods and variables � Subclass (child class) inherits all methods except constructor methods from superclass (parent class) � except constructor � Using reserved word super in subclass constructor tells � can just add new variables and methods Java to call appropriate constructor method of superclass 5 6

  2. Recap: Inheritance and Scope Recap: Method Overriding � Subclasses inherits but cannot directly access � If child class defines method with same name private fields or variables of superclass and signature as method in parent class � say child's version overrides parent's version � Protected variables can be directly accessed in favor of its own from declaring class and any classes derived from it 7 8 Recap: Object Behind the Scenes Recap: Overriding Variables � You can, but you shouldn't � All classes that aren't explicitly extended from � Possible for child class to declare variable a named class are by default extended from with same name as variable inherited from Object class parent class � Object class includes a toString() method � one in child class is called shadow variable � so... class header � confuses everyone! public class myClass � Child class already can gain access to � is actually same as inherited variable with same name public class myClass extends Object � there's no good reason to declare new variable with the same name 9 10 Objectives Recap: Method Overloading and Overriding � Method overloading: "easy" polymorphism � Understanding when and how to use abstract classes � in any class can use same name for several different (but hopefully related) methods � Understanding tradeoffs between interfaces � methods must have different signatures so that and inheritance 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 11 12

  3. A New Wrinkle If We Have This Class Hierarchy... � Expand vending machine Coke empire to include French fry Machine machines is-a � is a French fry machine a Coke subclass of Coke Machine? Machine2000 is-a Coke MachineUA 13 14 ...Does This Make Sense? Does This Make More Sense? Generic Coke Vending Machine Machine is-a is-a is-a is-a French Fry Coke French Fry Coke Machine Machine2000 Machine Machine is-a is-a Coke Coke MachineUA Machine2000 is-a Coke MachineUA 15 16 Does This Make More Sense? Does This Make More Sense? Generic Generic Vending Vending Machine Machine is-a is-a is-a is-a is-a is-a French Fry Coke Beer French Fry Coke Pizza Machine Machine Machine Machine Machine Machine is-a is-a � Yes Yes � � especially if we're thinking of adding all � especially if we're thinking of adding Coke Coke kinds of vending machines... all kinds of vending machines... Machine2000 Machine2000 � want our classes to be more specific as is-a is-a we go down class hierarchy � is French Fry Machine more or less Coke Coke specific than Coke Machine? MachineUA MachineUA � neither, both specific versions of generic Vending Machine class 17 18

  4. Does This Make More Sense? Inheritance Solution public class GenericVendingMachine Generic { Vending private int numberOfItems; private double cashIn; Machine is-a is-a is-a is-a public GenericVendingMachine() { numberOfItems = 0; Beer French Fry Coke Pizza } Machine Machine Machine Machine public boolean vendItem() { is-a � One way: make a VendingMachine boolean result; if (numberOfItems > 0) interface like last week { Coke numberOfItems--; � Another way... Machine2000 result = true; } is-a else { result = false; } Coke return result; MachineUA } 19 20 Inheritance Solution Inheritance Solution public void loadItems(int n) public class CokeMachine3 extends GenericVendingMachine { { numberOfItems = n; public CokeMachine3() } { super(); public int getNumberOfItems() } { return numberOfItems; public CokeMachine3(int n) } { super(); } this.loadItems(n); } public void buyCoke() { if (this.vendItem()) { System.out.println("Have a nice frosty Coca-Cola!"); System.out.println(this.getNumberOfItems() + " cans of Coke remaining"); } else { System.out.println("Sorry, sold out"); } } 21 22 Inheritance Solution Inheritance Solution public void loadCoke(int n) public class CokeMachine2000 extends CokeMachine3 { { this.loadItems(this.getNumberOfItems() + n); public CokeMachine2000() System.out.println("Adding " + n + { " ice cold cans of Coke to this machine"); super(); } } } public CokeMachine2000(int n) { super(); this.loadItems(n); } public void loadCoke(int n) { super.loadCoke(n); System.out.println("Loading in the new millennium!"); } } 23 24

  5. Inheritance From Generic Objects Inheritance From Generic Objects Generic Generic Vending Vending Machine Machine is-a is-a is-a is-a is-a is-a French Fry Coke Beer French Fry Coke Pizza Machine Machine Machine Machine Machine Machine is-a is-a � Want generic VendingMachine class � Will we ever want to instantiate a generic Vending Machine class? � don’t actually use to generate objects Coke Coke � will we ever need to make generic Machine2000 Machine2000 � use as template for specific actual Vending Machine object? classes like FrenchFryMachine and is-a is-a CokeMachine Coke Coke MachineUA MachineUA 25 26 Inheritance From Generic Objects Inheritance From Generic Objects � Introduced CokeMachineUA to combat Generic Vending vandalism and theft Machine is-a is-a � Could just add vandalize() methods to CM, is-a is-a CM2000, CMUA Beer French Fry Coke Pizza Machine Machine Machine Machine � but we want to ensure that all Vending is-a � Will we ever want to instantiate a generic Machines have vandalize() methods Vending Machine class? Coke � want all of them to be different � will we ever need to make generic Machine2000 Vending Machine object? � if put into base class at top, easy to have them is-a � No, not in our simulated vending world! identical � How would we use one? What would be Coke � no way to force method overriding a real-life equivalent? MachineUA 27 28 Abstract Classes Abstract Classes � Abstract class: not completely implemented � Abstract classes serve as place holders in class � Usually contains one or more abstract methods hierarchy � has no definition: specifies method that should be implemented � Abstract class typically used as partial description by subclasses inherited by all its descendants � just has header, does not provide actual implementation for � Description insufficient to be useful by itself that method � Abstract class uses abstract methods to specify what � cannot instantiated if defined properly interface to descendant classes must look like � Descendent classes supply additional information � without providing implementation details for methods that so that instantiation is meaningful make up interface � abstract class is generic concept in class hierarchy � Example: require that all subclasses of VendingMachine class implement vandalize() method � class becomes abstract by including the abstract � method might differ greatly between one subclass and another modifier in class header � use an abstract method 29 30

Recommend


More recommend