Interfaces

From CSE231 Wiki
Jump to navigation Jump to search

Interfaces in Java can be a powerful tool, allowing you to abstract away implementation details. For CSE 231, you will never be required to write your own interfaces for any of our assignments; however, whenever you use lambdas, you are implementing an existing interface, and it can be useful to understand exactly what an interface is and what lambdas are doing behind the scenes.

Terminology

An interface is, most simply, a group of related methods with empty bodies. An interface is used to describe methods that a class should have, without actually giving the details on exactly what the class does internally. In the same way that a class is a blueprint for an object, an interface is a blueprint for a class.

To implement a method is to write the code that describes what it does internally. An umimplemented or abstract method is one that has no method body. All of the methods in an interface are abstract.

An implementation or implementing class is a normal Java class that implements the methods in the interface. In other words, the interface describes what the methods are, and the implementation describes what those methods do internally. All of the methods defined in an interface must be implemented in the implemented class (unless the class is abstract). The power of an interface comes from the fact that multiple classes can implement the same interface.

A lambda is a special case of implementing class for an interface with only one method. Lambdas are discussed more extensively on the Lambdas page.

Examples

The Java Tutorials has a simple example of a Bicycle interface and a ACMEBicycle implementing class. Notice how the methods in the Bicycle interface don't have bodies. In this example, the interface isn't particularly useful; if an interface only has one implementing class, you may as well just have the implementing class.

TutorialsPoint has a more in-depth explanation of how interfaces work in Java. It's recommended that you read through this if you don't understand how interfaces work.

However, these examples are still rather contrived. For a real example of a Java interface, take a look at the List interface. If you look at the source, you will notice that none of the methods have bodies; they're simply definitions of methods that a list should have. The Java Development Kit has several implementations of the List interface, most notably ArrayList and LinkedList. Internally, these classes store data very differently. ArrayList has an array that stores elements, while LinkedList has a chain of list "nodes." Because both of these classes implement the List interface however, either one could be passed into a method that takes a List as a parameter.

An Added Benefit

A key feature of interfaces is that the implementing classes don't have to be from the same package or library as the interface. In other words, you could create your own List implementation, so long as it implemented all of the methods in the List interface. The benefit to this is that a method can simply require a List as a parameter, and any class that implements List will be accepted. If you created your own List implementation that stores data in a unique way, you could still call the Collections.sort() method on it. This powerful construct is what allows lambdas to function.