7/2/20091More Inheritance7/2/2009Important Dates• Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab• Review Session– Sunday 7/05/2009 – 306 Soda 1-4pm • Midterm 1– Tuesday 7/07/2009 – 10 Evans 5-6pm– Covers everything through Monday’s lab• Project 2 released– Thursday 7/09/2009• Project 1 due– Monday 7/13/2009 – 10pmequals(Object o)AnimalDogDalmatian PoodleCat• Did you understand Wednesday’s lab?Animal a1 = new Dog();Animal a2 = new Dog();Dog d1 = new Dog();a1.sniff(d1);d1.sniff(a1);a1.sniff(a2);← Uses a1’s dynamic type← Uses a1’s static type← Uses a1’s dynamic typeUses a2’s static typeInheritance • Compilation:– CALLER: It makes sure that the static type of the object has the appropriate method– ARGS: It makes sure that the method takes in the static type of the arguments• Run-time:– CALLER: When you call a method on an object it looks for the method starting at the object’s dynamic type– ARGS: When you pass an object as an argument, it looks for a method with that static typeDogagebites 0truemainAnimal a1Animal a2Dog d1 Animal a1 = new Dog();Animal a2 = new Dog();Dog d1 = new Dog();a1.sniff(d1);d1.sniff(a1);a1.sniff(a2);Dogagebites 0trueDogagebites 0true7/2/20092What about equals(Object o)• ArrayList was using an Object reference Object o1 = new Animal();Object o2 = new Animal();o1.equals(o2);• If others will call your methods with more generic references, you want to provide a method that takes in an Object AnimalsPetsEat BananasAnimalHuman Monkey Ant Eater Cat DogDalmatian PoodleInterfaces• I have a GREAT idea! • Everyone will want to make Pets that can – eatKibble()– sitOnLap()• How?!?!– I don’t care!!!!The Pet interfaceDog can implementthe Pet interfaceCat can implementthe Pet interface7/2/20093We can have a Petremote control(Pet reference)Pet p1 = new Dog();Pet p2 = new Cat();p1.eatKibble();p2.sitOnLap();Can we have a Pet object?Pet p1 = new Pet();Interfaces can be cool!!!Example• The sort method of the Array class promises to sort an array of objects, but under one condition: the objects in the array must implement the Comparable interface:public interface Comparable {int compareTo(Object other);}Summary of Interfaces• Interfaces don’t implement ANY methods– Just put a semicolon at the end of the method• Classes can implement multiple interfaces• To implement an interface you must write all of the methods that the interface definesCookies!CookieGirl Scout CookieThin mint ShortbreadChocolate Chip7/2/20094Abstract Classes (less lazy than Interfaces)• I have a GREAT idea! • Everyone will want to make Cookies that can have these methods:– ingredients()– isDelicious()• How?!?!– isDelicious() is pretty simple, I’ll write that one– ingredients() ?!? Sounds too hard! I’ll make that abstractAbstract Class CookieChocolateChipCookie can extend the Abstract class CookieWe can have a Cookie remote control(Cookie reference)Can we have a Cookie object?Cookie p1 = new Cookie();Abstract Class Summary• Label an Abstract class as abstract• Label any methods that you don’t want to implement as abstract– Your children MUST write all of the abstractmethods• Instance variables in the abstract class will be available in the child class• You can only extend one Class (abstract or
View Full Document