Difference between revisions of "Interfaces"

From CSE231 Wiki
Jump to navigation Jump to search
(Created page with "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 o...")
 
m
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
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.
 
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 [https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html lambda] is a special case of implementing class for an interface with only one method. Lambdas are discussed more extensively on the Lambdas page.
+
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 ==
 
== Examples ==
Line 18: Line 18:
  
 
However, these examples are still rather contrived. For a real example of a Java interface, take a look at the [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List] interface. If you look at [http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/List.java 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 [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList] and [https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html 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.
 
However, these examples are still rather contrived. For a real example of a Java interface, take a look at the [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List] interface. If you look at [http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/List.java 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 [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList] and [https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html 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 [https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List) Collections.sort()] method on it. This powerful construct is what allows lambdas to function.

Latest revision as of 05:16, 31 January 2017

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.