CS49J Lesson 18 – Interfaces
Ch 10: 10.1-10.3

When you come in
- Connect to the Internet
- Log in to Piazza
- Start Eclipse or your IDE
- Navigate to laughton.com/obrien/sjsu/
cs49j/lessons and open this lesson
Interfaces
Allows differernt objects to make use of the same service
- a lightbulb socket
- a USB port
- a handle for a set of tools
Concept - Java interfaces
- We can also have software interfaces. In Java we use the keyword interface
- Syntax
public interface interfaceName
{
//one or more methods that describe what the implementing class has to provide
//public by default
// no body. They are abstract
}
- Example
public interface Measurable
{
double getMeasure();
}
- You can write your own interfaces as Dr Horstmann does in your book
- Most often at this stage you will use interfaces that have already been written
Concept - Implementing an interface
There are two parts
- Indicate that the class uses the interface with the implements keyword
public class MyClass implements interfaceName
For example
public class Product implements Measurable
- Provide the body for the methods declared in the intefrace definition
For example:
T
o complete the implementation of the Measuable interface in the Product class
public double getMeasure()
{
return price;
}
Concept - Simplified Comparable interface
Activity - Comparable interface in the String class
Record your participation in Piazza clicker question Lesson18 Q1
- String class implements Comparable. How could you find out for yourself if it implemented Comparable?
- What do these comparisons return?
- "hello".compareTo("help")
- "help".compareTo("hello")
- "help".compareTo("help")
- "Help".compareTo("apple")
- "999".compareTo("apple")
Why implement the Comparable interface in your classes
- Allows you to use services provided by utility classes in the Java library
- Arrays.sort for arrays or Collections.sort for ArrayLists
Concept - Implementing the Comparable interface
- State that the class will implement the Comparable interface
public class MyClass implements Comparable
- Implement the compareTo method - with this exact signature
public int compareTo(Object otherObject)
- First you will need to cast otherObject to a MyClass to get a MyClass reference
MyClass other = (MyClass)otherObject;
- Then you will compare the instance variables of the two obects in some way returning
- a negative number if this object should come before other,
- zero if this object and other are the same
- a positive number if other should come before this object.
Activity - Try to sort BankAccounts
Record your participation in Piazza clicker question Lesson18 Q2
- Download comparable_interface.zip and make a project
- Make the project without a bunch of copy/paste
- Look at ComparableTester.
- We want to sort the array of BankAccounts using Arrays.sort.
- What happens when you compile ComparableTester?
- What happens when execute the main method?
- Why?
Activity - Implementing Comparable interface in BankAccount
Record your participation in Piazza clicker question Lesson18 Q3
Fix the problem by implementing the Comparable interface in BankAccount.
- The class header needs to specify that we are implementing Comparable interface
- BankAccount already implements one interface
- To add another add a comma and the new interface name
, Comparable
- Won't compile
- Add the required method from the interface, compareTo(Object otherObject), as a stub. Compile
- Now implement the body of the compareTo method
- You will need to cast otherObject to a BankAccount
- order by
balance
- If the balances are equal, order by accountId
Activity - A trick for comparing doubles
Record your participation in Piazza clicker question Lesson18 Q4
Activity - Implementing Comparable interface in Student
Record your participation in Piazza clicker question Lesson18 Q5
Add the Comparable interface to the Student class. Students are ordered by GPA. If the GPA's are the same, they are ordered by name. Uncomment the code in ComparableTester that creates the ArrayList.
- Got it
- Got it but it was hard
- Didn't get it
Activity - Casting
Record your participation in Piazza clicker question Lesson18 Q6
In ComparableTester, Student objects are in a Comparable array.
- Look at the code.
- This is valid. Student is-a Comparable
- uncomment the code for testing and run
- What happens? Why?
- Fix the problem by casting
Activity - Creating Comparable objects
Record your participation in Piazza clicker question Lesson18 Q7
What will happen when you try to compile these statement
- Comparable c = new Comparable();
- Comparable c = new Student("Alice", 3.2);
- Comparable c1 = new Rectangle(10, 20, 30, 40); (Look in the Rectangle API)
Activity - Limits of the Comparable interface
Record your participation in Piazza clicker question Lesson18 Q8
Can you use the Arrays.sort to find the Rectangle with the largest area in an array of rectangles by sorting the array?
- Yes
- No
- It depends