DOC PREVIEW
UT CS 307 - Interfaces and Abstract Classes

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 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 37 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 37 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 37 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 37 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 37 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 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 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 Inheritance88The 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 member88Java 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 Inheritance8Sltilihitlld8Suppose multiple inheritance was allowedpublic class GradTA extends Faculty, GradStudent8Suppose Faculty overrides toString and that8Suppose Faculty overrides toString and that GradStudent overrides toString as wellGradTA ta1 = new GradTA();GradTA ta1 = new GradTA();System.out.println( ta1.toString() );8What is the problemWhat is the problem8Certainly 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 Inheritance88Java does not allow multiple inheritance– syntax headaches not worth the benefits8Java has a mechanism to allow specification of a data type with NO implementationyp p– interfaces8Pure 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 Interface88What if we wanted to specify the operations for a List, but no implementation?8Allow for multiple, different implementations.8Provides 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 Classes7Interfaces88All methods in interfaces are public and abstract– can leave off those modifiers in method headers8No constructors8No instance variables8can have class constants8can have class constantspublic static final int DEFAULT_SIDES = 6CS 307 Fundamentals of Computer Science Interfaces and Abstract Classes8Implementing Interfaces88A class inherits (extends) exactly one other class, but …8A class can implement as many interfaces as it likespublic class ArrayList implements List8A 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 abstract8interfaces can extend other interfacesCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes9interfaces can extend other interfacesWhy interfaces?8If llh i fbt tdt t8Interfaces 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 allowed8Interfaces 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 concerns8allow a form of multiple inheritanceCS 307 Fundamentals of Computer Science Interfaces and Abstract Classes108allow a form of multiple inheritanceThe Comparable Interface88The Java Standard Library contains a number of interfaces– names are italicized in the class listing8One 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 );}8compareTo 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 Comparable88Any 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 interface8Back to the ClosedShape example8Suppose 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 compareTo88Suppose we have a class to model playing cards– Ace of Spades, King of Hearts, Two of Clubs8each card has a suit and a value, represented by ints8this 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 Polymorphism88Interfaces may be used as the data type for object variables8Can’t simply create objects of that type8Can refer to any objects that implement the8Can refer to any objects that implement the interface or


View Full Document

UT CS 307 - Interfaces and Abstract Classes

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 Interfaces and Abstract Classes
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 Interfaces and Abstract Classes 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 Interfaces and Abstract Classes 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?