INTERFACES More OO Concepts
Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism
Interfaces What functions does an alarm clock have?
What type of data can be sorted?
How can we sort? JimBarryDoug SORT CompareTo Jim.compareTo(Barry) >0 Barry.compareTo(Doug) <0 Jim.compareTo(Doug) >0 (may do other compares, depends on sort algorithm) JimBarryDoug
How can we sort? Jim 3.95 Barry 3.98 Doug 3.02 SORT CompareTo Jim.compareTo(Barry) <0 Barry.compareTo(Doug) >0 Jim.compareTo(Doug) >0 Doug 3.02 Jim 3.95 Barry 3.98 Interface: Comparable Requires: public int compareTo(Object) - old style public int compareTo (ObjType)
In Code public class Student implements Comparable { private String name; private double gpa; public Student(String name, double gpa) { super(); this.name = name; this.gpa = gpa; } public int compareTo(Student other) { //return name.compareTo(other.name); if (gpa < other.gpa) return -1; else if (gpa > other.gpa) return 1; else return 0; public String toString() { return "Student [gpa=" + gpa + ", name=" + name + "]"; } public class SortDemo { public static void main(String[] args) { ArrayList students = new ArrayList (); students.add(new Student("Jim", 3.95)); students.add(new Student("Barry", 3.98)); students.add(new Student("Doug", 3.02)); Collections.sort(students); for (Student s : students) System.out.println(s); }
Where else are interfaces used? actionPerformed actionListener mouselistener mouseClicked mouseEntered mouseExited mousePressed mouseReleased Collections – discussed soon
Can I write my own Interface? Yes! And you should! When is it appropriate? –Whenever you have a function or set of functions that are likely to be implemented in multiple ways (like compareTo) –And it’s not appropriate to use inheritance (i.e., not just overriding parent class) –Think: Would it be accurate to say a Student is-a Comparable? –NO! Comparable just ensures students can be compared.
Quick Exercise Assume you’re writing an adventure game. Lots of different game pieces move – so you might have a Moveable interface, with a method named move. With a partner, brainstorm what other behaviors might be common across completely different types of pieces. I will ask every pair for one suggestion (there may be repeats)
A few details … We know: Interface specifies common set of operations Most methods are abstract (like prototypes), no implementation. Why?* Must be public. Why? Interfaces may not have instance fields (consider: never instantiate an interface directly. Think about Comparable…) Interface may have constants. Why? Keyword: implements Class can only extend one class, but may implement as many as you want. *changed in Java 8
Interfaces in Java 8 Can now have default methods Added so that interfaces could be updated without breaking lots of existing code. In general, original philosophy (only abstract methods) will still be the best approach for many situations We won’t cover default methods make-pandas-sad-and-your-teammates-angry/
Design Decisions Should AlarmClock be an interface or an abstract class? Writing a CAD-like program. Lots of different objects (e.g., windows, walls, walkways) have an area. How to handle? Ask yourself: –Does “is-a” apply? –Are there attributes in common, or just behaviors?
Another Interface Example public interface Measureable { double getMeasure(); } public class BankAccount implements Measurable { public double getMeasure() { return balance; }... private double balance; } public class Student implements Comparable, Measurable { private String name; private double public double getMeasure() { return gpa; } // plus all the other stuff } Can implement more than one interface! keyword public required, default is package
Interface Example, continued public class DataSet { public void add(Measurable x) { sum = sum + x.getMeasure(); if (count == 0 || max.getMeasure() < x.getMeasure()) max = x; count++; } public Measureable getMaximum() { return max; } private double sum; private Measurable max; private int count; }
Interface Example, continued public static void main(String[] args) { BankAccount mine = new BankAccount(500); BankAccount yours = new BankAccount(400); DataSet data = new DataSet(); data.add(mine); data.add(yours); System.out.println (data.getMaximum().getMeasure()); DataSet data2 = new DataSet(); data2.add(new Student("Goofy", 3.9)); data2.add(new Student("Mickey Mouse", 3.0)); data2.add(new Student("Donald Duck", 2.5)); System.out.println (data2.getMaximum().getMeasure()); }
Polymorphism Measurable x = new BankAccount(1000); double m = x.getMeasure(); x = new Coin(0.1, “dime”); m = x.getMeasure(); JVM locates correct method. What did we do in C++? Overloading method – early binding (e.g., default vs 1-parameter constructor; static) Polymorphism – late binding (dynamic) How is correct method executed?
UML Diagram BankAccountCoin > Measurable DataSet stereotype indicator implements (triangular tip, dotted line) uses (open arrow tip)
javadoc comments Remember the API had a standard format. User-defined classes can easily create html documentation in that same format, by inserting comments that meet certain specifications: Start with /** First sentence describes tags specify @version) run javadoc from command line or Eclipse to generate html pages