140 likes | 638 Views
CIS110-203 Intro to Computer Science Lab #11 TA: Catherine Stocker Mentor: Jay Fiddelman University of Pennsylvania 15 November 2007 Today’s Agenda Review Review Question Lab Work Space Invaders Monitors… Turn ‘em off General Stuff How is the homework going?
E N D
CIS110-203Intro to Computer Science Lab #11 TA: Catherine Stocker Mentor: Jay Fiddelman University of Pennsylvania 15 November 2007
Today’s Agenda • Review • Review Question • Lab Work • Space Invaders
Monitors… • Turn ‘em off
General Stuff • How is the homework going? • Exam next Tuesday. Review sessions this weekend. • Projects start when we get back from Thanksgiving break.
What’s the point of interfaces if they’re just blank? When should we use them and why are they helpful? • Point of extending classes: • Polymorphic data structures (arrays, lists, etc) and methods • Code Reuse • Point of implementing interfaces: • Polymorphic data structures (arrays, lists, etc) and methods • Can implement multiple (because no code reuse) • Polymorphic data structures and methods • Implementing an interface is like making a promise • Ex. Anything implementing the List interface promises to be able to add, remove, get, etc • Anything that makes that promise can be used interchangably (sort of)
What’s the point of interfaces… • Back to the Governator example: • Say you have a Governator class. • We all know a Governator is both a Governor and a Terminator. • Say the Governor and Terminator CLASSES each have a resolveConflict() method • Which resolveConflict method is used when the Governator class extends both classes? Answer: This is why you’re not allowed to extend multiple classes.
What’s the point of interfaces … • BUT THAT’S NOT FAIR! 50% of the reason we extend classes is to allow for polymorphic code! • What if we want to make two separate arrays, one that holds Governors and one that holds Terminators? Which one does he belong in? • What if we want to pass a Governer to a makeLaws() method and a Terminator to a killPeople() method? How can he do both? • Never fear…interfaces are here!
What’s the point of interfaces … • We’re allowed to implement MULTIPLE interfaces!!! • Since the methods of an interface have no body (these type of methods are called abstract methods) then if we implement a Governor and Terminator INTERFACE we don’t run into the problem of which resolveConflict() to use. We just implement our own Governator specific resolveConflict(). • BUT we have still made the Promise that we will implement all of the methods declared in each interface. So if we want to pass it to method that requires a Governor as an argument, that’s ok because we know it will have all of the functionality that a Governor can have. Same with data structures – if we want to store it in a Governor array and a Terminator array, it has promised to be able to act like both!
What’s the point of interfaces … • One more thing…sometimes we just don’t care to implement the interface method. • If you have an Animal class, how do you implement a makeNoise() method? “bark”? “meow”? “neigh”? • What about a Horse class? makeNoise() should “neigh”. • So Animal needs only be an interface that promises that all Animals will do something – makeNoise(), sleep(), eat() • Then…you can have a method: • public void scareAnimals(Animal[] a) { for(int i=0 ; i<a.length ; i++) a[i].makeNoise(); } Which may look something like: “bark” “bark” “neigh”
How do we code interfaces? • My Animal, Dog and Horse code: • public interface Animal { public void makeNoise(); } • public class Dog implements Animal { public Dog() {} public void makeNoise() {System.out.println("bark");} } • public class Horse implements Animal { public Horse() {} public void makeNoise() {System.out.println(“neigh");} }
Put all of that together…(and review arrays of objects) • public class AnimalExample { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Dog(); //upcasting a[1] = new Dog(); a[2] = new Horse(); scareAnimals(a); } public static void scareAnimals(Animal[] a) { for(int i=0 ; i<a.length ; i++) { a[i].makeNoise(); } } }
upcasting v. downcasting • Given a class Person (also applies to interfaces, but since you cannot instantiate an interface, I’ll use classes for simplicity) and the class that inherits from it Student: • Upcasting (GOOD) • Casting to the class/interface “above” (parent). Going from more specific (Student) to more general (Person) • Person[] p = new Person[3];p[0] = new Student(); (automatically happens here) • Person p; Student s; p = (Person) s; • Downcasting (BE CAREFUL) • Casting to the class “below” (child). Going from general to specific….only if it was specific first. • Person p; Student s1,s2; p=(Student) s1; s2=(Student)p; • Person p; Student s1; s1=p; (NOT OK)
Lab Work • Work alone or together • Linked listshttp://www.seas.upenn.edu/~cis1xx/projects/LinkedList_NonGeneric/ • Review Problem • Space Invaders (only if you’ve finished the other two)
Later… • Finish up Space Invaders • Review Sheet by Sunday • Good luck on the exam!