Lesson 19 – Interfaces

Java Concepts

Interfaces, event listeners

Teacher Materials

Get Access

Classes are used to encapsulate variables and methods that logically belong together. Someone can use a class without understanding all of the details of the code inside it. For example, you probably use the Finch methods without understanding exactly how they work! 

Interfaces take this one step further. An interface is a list of abstract methods that is essentially a contract. A class that implements the interface promises to provide each of the methods specified by the interface. For example, suppose that we want to standardize how software interacts with robots. We can create an interface named StandardRobot that outlines our requirements. To do this, we use the keyword interface and provide a specification for each method that a class must provide to implement this interface. This sample interface requires only two methods, straight25cm() and turnLeft90().

We can use the keyword implements to define a class named StandardFinch that fulfills the requirements of the StandardRobot interface. Notice that we get an error when we declare this class because we have not provided the method required by the interface.

Once we have provided the required methods, the error goes away.

The advantage of an interface is that you can write a method that accepts more than one type. The square() method below accepts an object of type StandardRobot. This means that square() does not care about the exact type of the object passed to it. All it needs to know is that the class meets the requirements of the StandardRobot interface.

Exercise 1

Create a StandardFinch class that implements the interface given in StandardRobot.java (note that this file contains more methods than the version shown above). Then test your code with StandardFinchDriver.java. You should not change the code in either of the files you are given.

Exercise 2

How can you extend the StandardRobot interface? Expand on your work in Exercise 1 to add functionality to the interface. For example, you can add more methods that use the sensors, or add control for the lights or buzzer. Remember to update StandardFinchDriver.java to test the new functionality.

One common reason to use an interface when writing software is to listen for events generated by the system. For example, if a class implements the KeyListener interface, Java will notify the class when the user presses a key on the keyboard. This does not pause the program to wait for user input, like nextInt() in the Scanner class does. Instead, the program runs and notifies the class when a key is pressed or released. The methods required by the KeyListener interface tell the program what to do when these notifications arrive.

For example, we can create a class that controls the Finch with commands from the keyboard. This requires some code to generate a graphics window, as noted in the sample code below. This FinchKeyListener class implements the KeyListener interface, which requires three methods: keyPressed(), keyReleased(), and keyTyped(). This class isn’t using keyTyped(), so the body of that method is empty. The keyPressed() method makes the Finch move forward when the user presses the up key. When the user releases any key, the keyReleased() method stops the Finch.

Exercise 3

Try out the code in FinchKeyListener.java and FinchKeyListenerDriver.java. Make sure that you can control the Finch by pressing and releasing the up key. Then modify the keyPressed() implementation to make the Finch respond to more keys! You can use other keys for other movements, to light up the Finch, etc. More information about which key codes correspond to which keys can be found here.

Back to Top