Interfaces
> Click or hit Control-Enter to run Example.main above
Interfaces
Interfaces are an incredibly important idea when building computer programs and systems.
What Is An Interface?
Interface: a shared boundary across which two or more separate components of a computer system exchange information.
-
Interfaces can be between two pieces of software, between software and hardware, between computers and their users, or between various permutation of these components.
-
Interfaces enable different parts of a system to interact in a structured way.
Examples of Computer Interfaces
-
Software-software: between the test cases that we write and the code that you complete for each MP or homework problem.
-
Software-hardware: between my laptop and the Chromecast that is displaying today’s lecture slides.
-
Computer-user: computer displays, keyboards, pointing devices, and other peripherals.
Software Interfaces
We’re going to focus on software interfaces, and specifically on interfaces in Java.
-
However, interfaces are not a Java- or language-specific idea.
-
Some languages—like Java, Go, and others—include a specific notion of interfaces as part of the language.
-
For other languages this is done by convention.
-
But all software development involves interfaces, regardless of what language you are using.
Note: Every Java Object Has An Interface
Even Java classes that don’t implements
a Java interface provide an interface.
The interface to a Java class
is the the set of methods that it
provides.
Interface Documentation
Interfaces are also a place where we need excellent documentation.
-
This facilitates communication between users of an interface and providers of an interface.
-
This is exactly what Javadoc is for.
Java Interfaces
public interface Add {
int add(int first, int second);
}
-
Java interfaces look like empty objects: just method signatures with no implementation.
-
Interfaces can declare both methods and variables.
-
However, interfaces variables are
public static final
by default, meaning that they are only useful for declaring constants.
Implementing Interfaces
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
}
-
Interfaces don’t do anything useful by themselves. Instead, they have to be implemented by specific classes.
-
To declare that a class implements an interface you use the
implements
keyword as shown above. -
To implement an interface you must implement all of the methods that it declares.
> Click or hit Control-Enter to run Example.main above
Interface Casting
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
public int multiply(int first, int second) {
return first * second;
}
}
Add add = new Adder();
System.out.println(add.add(10, 20));
// But this doesn't work because multiply is not part of the add interface
System.out.println(add.multiply(10, 20));
-
Similar to inheritance I can automatically cast an object reference to any interface that it implements.
-
However, if I do that I can no longer access methods that are not part of the interface.
> Click or hit Control-Enter to run Example.main above
Interfaces v. Inheritance
So far this seems very similar to inheritance and overloading.
-
The interface is like the parent class
-
implement
is likeextends
-
Providing your own implementation is like overriding a parent’s method
> Click or hit Control-Enter to run Example.main above
abstract
Methods
It’s actually even more similar than it seems.
Remember abstract
classes?
abstract
classes can also have abstract
methods:
public abstract class Add {
public abtract int add(int first, int second);
}
> Click or hit Control-Enter to run Example.main above
So Why Interfaces?
Added Flexibility
Sometimes we want to mix capabilities from different branches of the tree.
Multiple Inheritance
public interface Add {
int add(int first, int second);
}
public interface Subtract {
int subtract(int first, int second);
}
public class Mathy implements Add, Subtract {
public int add(int first, int second) {
return first + second;
}
public int subtract(int first, int second) {
return first - second;
}
}
Unlike inheritance, classes can implement multiple interfaces.
> Click or hit Control-Enter to run Example.main above
Interface as Contract
/**
* Compares this object with the specified object for order.
*
* Returns a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
*/
public interface Comparable {
int compareTo(Object other);
}
Interfaces represent a contract between the interface provider and the interface user.
The interface represents all that the two components on either side need to agree on for things to work correctly.
Interface as Contract
public interface Comparable {
int compareTo(Object other);
}
By implementing
Comparable
you commit to being able to compare two instances of your class.
Using this ability I can implement code that:
-
sorts an array containing instances of your class
-
finds the maximum or minimum value of multiple instances of your class
-
arranges instances of your class into a binary tree 1
Interface as Abstraction Barrier
public interface Comparable {
int compareTo(Object other);
}
Good interfaces also represent a barrier between two unrelated parts of a computer program or system.
-
If I implement
Comparable
I don’t need to worry about how my implementation is used, but suddenly my class will have many new desirable features -
If I use
Comparable
I don’t need to worry about how the interface is implemented but I know that I can correctly compare two objects
> Click or hit Control-Enter to run Example.main above