INTERFACESINTERFACES
More OO Concepts
Interface TopicsInterface Topics
Using an interface
Interface details
syntax
restrictions
Create your own interface
Remember polymorphism
InterfacesInterfaces
What functions does an alarm clockhave?
http://t3.gstatic.com/images?q=tbn:ANd9GcRG5U4jo_ejFIxAYmm3FN3ot5cich6DVuF56B4jtZm7UK9efcVA
http://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Exquisite-kalarm.png/120px-Exquisite-kalarm.png
http://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Nuvola_apps_kalarm.png/120px-Nuvola_apps_kalarm.png
What type of data can be sorted?What type of data can be sorted?
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
http://t0.gstatic.com/images?q=tbn:ANd9GcRYaN0kggQVp64A7s3z14koukXAo9q1SkVgUxnTeVNac_Aq1fB6vg
http://t3.gstatic.com/images?q=tbn:ANd9GcQgi30h8TK4dNGorHrkxCdXaRvxLok0GiwS2sLyUeMvoYla5oCv6g
http://t0.gstatic.com/images?q=tbn:ANd9GcRkapxkldHrSAcp0qfS51m7ndeoEs6cj8oUV6zRpQgCiGA3ZNgz
How can we sort?How can we sort?
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Jim
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Barry
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Doug
SORT
CompareTo
Jim.compareTo(Barry) >0
Barry.compareTo(Doug) <0
Jim.compareTo(Doug) >0
(may do other compares, depends on sort algorithm)
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Jim
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Barry
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Doug
How can we sort?How can we sort?
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Jim3.95
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Barry3.98
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Doug3.02
SORT
CompareTo
Jim.compareTo(Barry) <0
Barry.compareTo(Doug) >0
Jim.compareTo(Doug) >0
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Doug3.02
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Jim3.95
http://t2.gstatic.com/images?q=tbn:ANd9GcSwdofJzbv2zwD_OElCOalTxftl3f81tW4r0Er1KydMkbUfxLlcYA
Barry3.98
Interface: Comparable
Requires: public int compareTo(Object)  - old style
    public int compareTo<ObjType>(ObjType)
In CodeIn Code
public class Student implementsComparable<Student>{
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;
}
@Override
public String toString() {
  return "Student [gpa=" + gpa + ",name=" + name + "]";
}  }
public class SortDemo {
public static void main(String[]args) {
   ArrayList<Student> students = new
      ArrayList<Student>();
   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?Where else are interfaces used?
Displaying a Button with an Icon Label
actionPerformed
actionListener
http://t2.gstatic.com/images?q=tbn:ANd9GcQBjSRGAbVe_rj0V7Om7z6ZMFxa-nh0EoNn9Sseb-EZvscVGgGH
mouselistener
mouseClicked
mouseEntered
mouseExited
mousePressed
mouseReleased
Collections – discussed soon
Can write my own Interface?Can write my own Interface?
Yes! And you should!
When is it appropriate?
Whenever you have a function or set offunctions that are likely to be implemented inmultiple 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 Studentis-a Comparable?
NO! Comparable just ensures students can becompared.
Quick ExerciseQuick Exercise
Assume you’re writing an adventure game.
Lots of different game pieces move – soyou might have a Moveable interface, witha method named move.
With a partner, brainstorm what otherbehaviors might be common acrosscompletely different types of pieces.
I will ask every pair for one suggestion(there may be repeats)
few details few details 
We know: Interface specifies common setof operations
Most methods are abstract (likeprototypes), no implementation. Why?*
Must be public.  Why?
Interfaces may not have instance fields(consider: never instantiate an interfacedirectly. Think about Comparable…)
Interface may have constants. Why?
Keyword: implements
Class can only extend one class, but mayimplement as many as you want.
*changed in Java 8
Interfaces in Java 8Interfaces in Java 8
Can now have default methods
Added so that interfaces could beupdated without breaking lots ofexisting code.
In general, original philosophy (onlyabstract methods) will still be thebest approach for many situations
We won’t cover default methods
http://zeroturnaround.com/rebellabs/how-your-addiction-to-java-8-default-methods-may-make-pandas-sad-and-your-teammates-angry/
Design DecisionsDesign Decisions
Should AlarmClock be an interfaceor an abstract class?
Writing a CAD-like program. Lots ofdifferent objects (e.g., windows,walls, walkways) have an area. Howto handle?
Ask yourself:
Does “is-a” apply?
Are there attributes in common, or justbehaviors?
Another Interface ExampleAnother Interface Example
public interfaceMeasureable
{
double getMeasure();
}
public class BankAccountimplements Measurable
{
public double getMeasure()
{
return balance;
}
. . .
private double balance;
}
public class Student implementsComparable<Student>,Measurable {
private String name;
private double gpa;
@Override
public double getMeasure() {
return gpa;
}
   // plus all the other stuff
}
Can implement more than
one interface!
keyword public required,
default is package
Interface Example, continuedInterface 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, continuedInterface 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());
}
}
PolymorphismPolymorphism
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., defaultvs 1-parameter constructor; static)
Polymorphism – late binding (dynamic)
How is correct method executed?
UML DiagramUML Diagram
BankAccount
Coin
<<interface>>
Measurable
DataSet
stereotype
indicator
implements
(triangular tip,
dotted line)
uses
(open arrow tip)
javadoc commentsjavadoc comments
Remember the API had a standard format.User-defined classes can easily createhtml documentation in that same format,by inserting comments that meet certainspecifications:
Start with /**
First sentence describes purpose
@ tags specify information (e.g., @param,@return, @throws, @author, @version)
run javadoc from command line or Eclipseto generate html pages