interfaces more oo concepts. interface topics using an interface interface details –syntax...

19
INTERFACES INTERFACES More OO Concepts

Upload: brenda-wright

Post on 04-Jan-2016

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

INTERFACESINTERFACESMore OO Concepts

Page 2: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Interface TopicsInterface Topics

• Using an interface• Interface details

– syntax– restrictions

• Create your own interface• Remember polymorphism

Page 3: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

InterfacesInterfaces

• What functions does an alarm clock have?

Page 4: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

What type of data can be sorted?What type of data can be sorted?

Page 5: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

How can we sort?How can we sort?

Jim

Barry

Doug

SORT

CompareTo

Jim.compareTo(Barry) >0Barry.compareTo(Doug) <0Jim.compareTo(Doug) >0(may do other compares, depends on sort algorithm)

Jim

Barry

Doug

Page 6: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

How can we sort?How can we sort?

Jim 3.95

Barry 3.98

Doug 3.02

SORT

CompareTo

Jim.compareTo(Barry) <0Barry.compareTo(Doug) >0Jim.compareTo(Doug) >0

Doug 3.02

Jim 3.95

Barry 3.98

Interface: ComparableRequires: public int compareTo(Object) - old style

public int compareTo<ObjType>(ObjType)

Page 7: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

In CodeIn Codepublic class Student implements

Comparable<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);

}

}

Page 8: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Where else are interfaces used?Where else are interfaces used?

actionPerformed

actionListener

mouselistener

mouseClickedmouseEnteredmouseExitedmousePressedmouseReleased

Collections – discussed soon

Page 9: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Can I write my own Interface?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.

Page 10: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Quick ExerciseQuick 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)

Page 11: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

A few details …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

Page 12: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Interfaces 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

http://zeroturnaround.com/rebellabs/how-your-addiction-to-java-8-default-methods-may-make-pandas-sad-and-your-teammates-angry/

Page 13: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Design DecisionsDesign 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?

Page 14: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Another Interface ExampleAnother 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<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

Page 15: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Interface Example, continuedInterface Example, continuedpublic 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;

}

Page 16: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Interface Example, continuedInterface Example, continuedpublic 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());

}

}

Page 17: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

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., default vs 1-parameter constructor; static)

Polymorphism – late binding (dynamic)

How is correct method executed?

Page 18: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

UML DiagramUML Diagram

BankAccount Coin

<<interface>>MeasurableDataSet

stereotype indicator

implements(triangular tip,

dotted line)

uses(open arrow tip)

Page 19: INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

javadoc commentsjavadoc 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 purpose• @ tags specify information (e.g., @param,

@return, @throws, @author, @version)• run javadoc from command line or Eclipse

to generate html pages