Inheritance and InterfacesSingle inheritance in JavaMultiple InterfacesAlan Kay: Turing Award 2004MVC: Model, View, ControllerMVC: interfaces and inheritanceDoes SimpleViewer know Anagrams?Comparable and ComparatorCPS 1005.1Inheritance and InterfacesInheritance models an "is-a" relationshipA dog is a mammal, an ArrayList is a List, a square is a shape, …Write general programs to understand the abstraction, advantages?void doShape(Shape s) { System.out.println(s.area()); System.out.println(s.perimeter()); s.expand(2.0);}But a dog is also a quadruped, how can we deal with this?CPS 1005.2Single inheritance in JavaA class can extend only one class in JavaAll classes extend Object --- it's the root of the inheritance hierarchy treeCan extend something else (which extends Object), why?Why do we use inheritance in designing programs/systems?Facilitate code-reuse (what does that mean?)Ability to specialize and change behavior•If I could change how method foo() works, bar() is okDesign methods to call ours, even before we implement•Hollywood principle: don't call us, …CPS 1005.3Multiple InterfacesClasses (and interfaces) can implement multiple interfacesA dog is a mammal, a quadruped, a petHow come canine is different?What behavior do quadrupeds have? Pets have?An interface specifies the name (and signature) of methodsNo implementation, no state/fieldsYes for constantsIn this course, by convention, we'll always use interfacesEmphasize design before implementationUse abstract/default classes for code reuse, stateCPS 1005.4Alan Kay: Turing Award 2004"Simple things should be simple. Complex things should be possible". "The best way to predict the future is to invent it" Kay, because of his experience with children, his love of education, his diverse interests, and his genius, recognized that users can and should interact with a computer in different ways and should not be limited to only text. He was among the first to represent objects in a computer as pictures -- a metaphor that he further extended by developing the concept of object orientation. He is, clearly, one of the fathers of the modern PC http://ei.cs.vt.edu/~history/GASCH.KAY.HTMLCPS 1005.5MVC: Model, View, ControllerA model is the state and brains of a systemIn a game it's all the pieces and where they areIn a spreadsheet it's the data and the formulaeThe view is how we look at the modelSpread sheet has graphs, charts, cells, text, …Game has board, number of opponents, hit-points, …When the model changes, the views reflect the changesThe model tells the views how/if it has changedModel sends information to views ORView asks model for informationCPS 1005.6MVC: interfaces and inheritanceA model might have multiple viewsTell all the views "I've changed"Who manages the views? This requires state: store viewsWhy can't we keep this state in an interface?See IModel and AbstractModelOne specifies behavior, the other provides default Don’t rewrite code if we don't have to, maintaining views will be the same for all modelsSee IView and SimpleViewNo default/shared view state/behavior: text and GUICPS 1005.7Does SimpleViewer know Anagrams? What does the SimpleViewer know about its model?If we look at code, is there any application-specific logic?What if we wanted to play a game, start a new game?Control in MVC with SimpleViewer and IModelLoading a file calls initialize()Entering text calls process()Model calls view with messages, errors, and complete updateThis isn't complete general, but it's pretty genericFor this input, here's the outputCPS 1005.8Comparable and ComparatorBoth are interfaces, there is no default implementationContrast with .equals(), default implementation?Contrast with .toString(), default?Where do we define a Comparator?In its own .java file, nothing wrong with thatPrivate, used for implementation and not public behavior•Use a nested class, then decide on static or non-static•Non-static is part of an object, access inner fieldsHow do we use the Comparator?Sort, Sets, Maps (in the
View Full Document