DOC PREVIEW
CORNELL CS 211 - Interfaces and related issues

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 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 21 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 21 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 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 07 Interfaces and related issuesInterfaces and related issuesThe interfaceAbstract data typeType List211Implement list as a stackSlide 7Class invariant: describes the fields and the values they contain:Interface ComparableImplementing method compareTo In this case, the parameter of compareTo is expected to be an instance of Shape, because the method is defined in ShapeWhy is the interface concept useful?An interface acts like a type, and casts to and from an interface are allowed!Class and interface hierarchiesSlide 14Slide 15PowerPoint PresentationSlide 17Slide 18Slide 19Slide 20Slide 211Lecture 07 Interfaces and related issuesReading for these lectures: Weiss, Section 4.4 (The interface), p. 110.ProgramLive, Section 12.1In User Interface there are personal preferences, but there are rules (guidelines) about what is right or wrong. … Apple and Xerox basically modeled most of their controls after things that we are familiar with in the real world; then they made behaviors consistent to increase predictability. Radio-Buttons, Checkboxes, Sliders are all things that people have seen before. …Later, others (like NeXT, or some Unix folks) added controls (like tabs), they modeled them after things in the real world (like little tabs on folders, file drawers or in notebooks). Again, that interface was clear and intuitive.Microsoft has contributed one control to the interface world: the Combo-Box. And like almost all things Microsoft, it is a bad implementation of user interface. David Every, http://igeek.com/articles/Interface/ComboBox.txt2Interfaces and related issues[Note: next Thursday, a review of classes/ subclasses, etc. 80 people for, 15 against.]no multiple inheritance with classespublic class Student { public class Employee {… …} }public class StudentEmployeeextends Student, Employee { … }Multiple inheritance has been tried in several languages, but it never works correctly. Trouble with ambiguity. E.g. what if method getName is defined in both superclasses but they yield different values? No good theory of such multiple inheritance has been developed yet, so Java does not allow it.not allowed.doesn’t work.3An interface contains the result types (or void) and signatures of some methods. The methods are called “abstract” because their bodies are not present; the bodies are replaced by semi-colons. The methods are automatically public.public interface Comparable {/** = if this Object < ob then a negative integer if this Object = ob, then 0 if this Object > ob, then a positive integer */ int compareTo (Object ob);}The signature of a method consists of its name and its parameter declarations (enclosed in parentheses and separated by commas).The interface abstract method: body replaced by “;”no prefix (static, public, etc.)4Abstract data typeType: a bunch of values together with operations on them.We can write an interface that DEFINES such a type. We call it an “abstract data type” because we don’t give a concrete implementation of it, we just define it “abstractly”5Type List211/** A list is a sequence of Objects. */public interface List211 { /** Make the list empty */ void makeEmpty(); /** Add item e to the list */ void add(Object e); /** Delete an item from the list */ void delete(); /** = an item in the list */ Object getItem(); /** = the number of items in the list */ int size();}Operations are not only abstract but ambiguous. Doesn’t say which item to delete or where to add an item.6Implement list as a stack/** An instance is a stack s of Objects */public class Stack1 implements List211 { …}Because of the implements clause, class Stack1 MUST define all the methods that appear in interface List211. It is a syntactic error if class Stack1 does not define them.implements clause7Implement list as a stack/** An instance is a stack s of Objects */public class Stack1 implements List211 { /** Make the stack empty */ public void makeEmpty() { } /** add item e to the front of the stack */ public void add(Object e) { } /** delete item from the front of the stack */ public void delete() { } /** = the front item of the stack */ public Object getItem() { return null; } /** = the number of items in the stack */ public int size() { return 0; }}Specs made more specific. Bodies not written yet.8Class invariant: describes the fields and the values they contain:/** An instance is a stack s of Objects, with a maximum size */public class Stack1 implements List211 { /** Stack s appears in b[0..n-1], so that s contains n items. b[0] is the bottom and b[n-1] the top of the stack*/ public Object[] b; public int n; /** Constructor: empty stack of <= m items */ public Stack1(int m) { b= new Object[m]; n= 0; } …We turn to DrJava to look at this class in detail. The class will be available on the web.9public interface Comparable {/** = if this Object < ob then a negative integer if this Object = ob, then 0 if this Object > ob, then a positive integer */ int compareTo (Object ob);}It has ONE method, compareTo.Interface Comparable10public class Shape implements Comparable {…// = if this Object < ob then a negative integer // if this Object = ob, then 0// if this Object > ob, then a positive integer// (it’s expected that ob is really a Shape) int compareTo(Object ob) {if (this.area() < ((Shape)ob).area())return -1;if (this.area() == ((Shape)ob).area())return 0;return 1;}}(in this case, Shapes are ordered by their area)Implementing method compareTo In this case, the parameter of compareTois expected to be an instance of Shape, because the method is defined in Shape ob is castto Shape11public class ArrayMethods {// = index of the max value in nonempty bpublic static int max(Comparable[] b) {int j= 0;// {invariant: b[j] is the max of b[0..i-1]}for (int i= 1; i != b.length; i= i+1) {if (b[i].compareTo(b[j]) > 0)j= i;}return j;}}Shape[] s= new Shape[20];Integer[] x= new Integer[100];Fill s and x with values.int maxs= ArrayMethods.max(s);int maxx= ArrayMethods.max(x);Why is the interface concept useful? max will find the max of any array b whose base class implements Comparable!12public class ArrayMethods {// = index of the max value in nonempty bpublic static int max(Comparable[] b) {int j= 0;// {invariant: b[j] is the max of b[0..i-1]}for (int i= 1; i != b.length; i= i+1) {if (b[i].compareTo(b[j]) > 0)j= i;}return j;}}Shape[] s=


View Full Document

CORNELL CS 211 - Interfaces and related issues

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Interfaces and related issues
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 related issues 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 related issues 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?