OO Concepts - Colorado School of Mines

Download Report

Transcript OO Concepts - Colorado School of Mines

More OO Concepts
INTERFACES
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?
Jim
Barry
SORT
Barry
Doug
CompareTo
Doug
Jim.compareTo(Barry) >0
Barry.compareTo(Doug) <0
Jim
Jim.compareTo(Doug) >0
(may do other compares, depends on sort algorithm)
How can we sort?
Jim
3.95
SORT
Barry
3.98
CompareTo
Doug
3.02
Jim
3.95
Jim.compareTo(Barry) <0
Barry.compareTo(Doug) >0
Jim.compareTo(Doug) >0
Doug
3.02
Barry
3.98
Interface: Comparable
Requires: public int compareTo(Object) - old style
public int compareTo<ObjType>(ObjType)
In Code
public 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);
}
}
Where else are interfaces used?
actionPerformed
actionListener
mouselistener
Collections – discussed soon
mouseClicked
mouseEntered
mouseExited
mousePressed
mouseReleased
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 before we get serious…
• We know: Interface specifies common set
of operations
• All 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
http://zeroturnaround.com/rebellabs/how-your-addiction-to-java-8-default-methods-maymake-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();
}
keyword public required,
default is package
public class BankAccount
implements Measurable
{
public double getMeasure()
{
return balance;
}
. . .
private double balance;
}
Can implement more than
one interface!
public class Student implements
Comparable<Student>,
Measurable {
private String name;
private double gpa;
@Override
public double getMeasure() {
return gpa;
}
// plus all the other stuff
}
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”);
How is correct method executed?
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)
UML Diagram
<<interface>>
Measurable
DataSet
uses
(open arrow tip)
BankAccount
stereotype
indicator
implements
(triangular tip,
dotted line)
Coin
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 purpose
• @ tags specify information (e.g., @param,
@return, @throws, @author, @version)
• run javadoc from command line or Eclipse
to generate html pages