Ti7Topic 7Interfaces and AbstractInterfaces and Abstract Classes“I prefer Agassiz in the abstract,rather than in the concrete.”CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes1InterfacesInterfacesCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes2Multiple InheritanceThe are classes where the “is-a” test is true for more than one other class– a graduate teaching assistant is a graduate students–a graduate teaching assistant is a faculty memberJava requires all classes to inherit from exactly one other class– does not allow multiple inheritance– some object oriented languages doCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes3Problems with Multiple InheritanceSltilihitlldSuppose multiple inheritance was allowedpublic class GradTA extends Faculty, GradStudentSuppose Faculty overrides toString and thatSuppose Faculty overrides toString and that GradStudent overrides toString as wellGradTA ta1 = new GradTA();GradTA ta1 = new GradTA();System.out.println( ta1.toString() );What is the problemWhat is the problemCertainly possible to overcome the problem–provide access to both (scope resolution in C++)–provide access to both (scope resolution in C++)– require GradTA to pick a version of toString or override it itself (Eiffel)CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes4()Interfaces – Not quite Multiple InheritanceJava does not allow multiple inheritance– syntax headaches not worth the benefitsJava has a mechanism to allow specification of a data type with NO implementationyp p– interfacesPure DesignPure Design– allow a form of multiple inheritance without the possibility of conflicting implementationspossibility of conflicting implementationsCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes5A List InterfaceWhat if we wanted to specify the operations for a List, but no implementation?Allow for multiple, different implementations.Provides a way of creating abstractions.o des a ay o c eat gabst act o s– a central idea of computer science and programming. pg g– specify "what" without specifying "how"–"Abstraction is a mechanism and practice toAbstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. "CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes6Interface Syntaxii ipublic interface List{public void add(Object val);public int size();public Object get(int location);public void insert(int location,Object val);public void addAll(List other);public Object remove(int location);}CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes7InterfacesAll methods in interfaces are public and abstract– can leave off those modifiers in method headersNo constructorsNo instance variablescan have class constantscan have class constantspublic static final int DEFAULT_SIDES = 6CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes8Implementing InterfacesA class inherits (extends) exactly one other class, but …A class can implement as many interfaces as it likespublic class ArrayList implements ListA class that implements an interface mustA class that implements an interface must provide implementations of all method declared in the interface or the class must bedeclared in the interface or the class must be abstractinterfaces can extend other interfacesCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes9interfaces can extend other interfacesWhy interfaces?If llh i fbt tdt tInterfaces allow the creation of abstract data types– "A set of data values and associated operations that are precisely specified independent of any particularprecisely specified independent of any particular implementation. "– multiple implementations allowedInterfaces allow a class to be specified without worrying about the implementation– do design first– What will this data type do?D ’t b t i l t ti til d i i d–Don’t worry about implementation until design is done. – separation of concernsallow a form of multiple inheritanceCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes10allow a form of multiple inheritanceThe Comparable InterfaceThe Java Standard Library contains a number of interfaces– names are italicized in the class listingOne of the most important interfaces is the Comparable interfaceCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes11Comparable Interface version 1.4package java.langpublic interface Comparable{public int compareTo( Object other );public int compareTo( Object other );}compareTo should return an int <0 if the calling object is less than the parameter, 0 if they areobject is less than the parameter, 0 if they are equal, and an int >0 if the calling object is greater than the parameterCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes12Implementing ComparableAny class that has a natural ordering of its objects (that is objects of that type can be sorted based on some internal attribute) should implement the Comparable interfaceBack to the ClosedShape exampleSuppose we want to be able to sort Suppose e a o be ab e o soClosedShapes and it is to be based on areaCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes13Example compareToSuppose we have a class to model playing cards– Ace of Spades, King of Hearts, Two of Clubseach card has a suit and a value, represented by intsthis version of compareTo will compare values first and then pbreak ties with suitsCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes14compareTo in a Card classpublic class Card implements Comparable{public int compareTo(Object otherObject){C d th (C d) th Obj t{Card other = (Card)otherObject;int result = this.myRank - other.myRank;if(result == 0)result = this mySuit-other mySuit;result = this.mySuit -other.mySuit;return result}// other methods not shown// ot e et ods ot s o}Assume ints for ranks (2, 3, 4, 5, 6,...) and suits (0 isclubs, 1 is diamonds, 2 is hearts, 3 is spades).CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes15Interfaces and PolymorphismInterfaces may be used as the data type for object variablesCan’t simply create objects of that typeCan refer to
View Full Document