DOC PREVIEW
UT CS 307 - Lecture notes

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 InheritanceThe 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 memberJava 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 InheritanceSltilihitlldSuppose multiple inheritance was allowedpublic class GradTA extends Faculty, GradStudentSuppose Faculty overrides toString and thatSuppose 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 problemCertainly 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 InheritanceJava does not allow multiple inheritance– syntax headaches not worth the benefitsJava has a mechanism to allow specification of a data type with NO implementationyp p– interfacesPure 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 InterfaceWhat 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 Classes7InterfacesAll methods in interfaces are public and abstract– can leave off those modifiers in method headersNo constructorsNo instance variablescan have class constantscan have class constantspublic static final int DEFAULT_SIDES = 6CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes8Implementing InterfacesA class inherits (extends) exactly one other class, but …A class can implement as many interfaces as it likespublic class ArrayList implements ListA 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 abstractinterfaces can extend other interfacesCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes9interfaces can extend other interfacesWhy interfaces?If llh i fbt tdt tInterfaces 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 allowedInterfaces 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 concernsallow a form of multiple inheritanceCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes10allow a form of multiple inheritanceThe Comparable InterfaceThe Java Standard Library contains a number of interfaces– names are italicized in the class listingOne 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 ComparableAny 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 interfaceBack to the ClosedShape exampleSuppose 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 compareToSuppose we have a class to model playing cards– Ace of Spades, King of Hearts, Two of Clubseach card has a suit and a value, represented by intsthis 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 PolymorphismInterfaces may be used as the data type for object variablesCan’t simply create objects of that typeCan refer to


View Full Document

UT CS 307 - Lecture notes

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?