Module 6b: Interfaces
Objectives
By the end of this module, you will be able to:
- Recognize the components and utility of an interface.
- Interpret the instructions for implementing an interface.
- Determine the correct steps to implement an interface.
- Complete the implementation of a basic interface.
Let's say that we want to write a class using the following requirements:
- First, the class must be named after an animal;
- Second, the class must have three methods that print a message related to the following actions: eating, moving, and making a sound;
- Third, the class must have one method that returns information about the number of legs of the animal.
How would you write it?
Here is one possible way of designing the class:
/* File name : Snake.java */ public class Snake { public static void eats() { System.out.println("Snake constricts and then eats"); } public static void moves() { System.out.println("Snake slithers"); } public static void sound() { System.out.println("Sssssss-Sssssss"); } public static boolean hasLegs() { return false; } public static void main(String args[]) { eats(); moves(); sound(); System.out.println( hasLegs() ); } }
Here is another possible way of writing this class:
/* File name : Dog.java */ public class Dog { public static void eat() { System.out.println("Dog eats"); } public static void travel() { System.out.println("Dog travels"); } public static void sound() { System.out.println("Woof-woof"); } public static int numOfLegs() { return 4; } public static void main(String args[]) { eat(); travel(); sound(); System.out.println( numOfLegs() ); } }
What are the differences?
- While the structure of the overall class is met, the details are different.
- The names are different.
- Three of the four methods are named differently.
- The parameter types returned are different.
- The instructions inside the methods are different.
Which would you choose? why?
Using Interfaces to set structure
In Java, there is a way to enforce that multiple implementations follow the same rules. In the same way that a method is a collection of instructions, and that a class is a collection of methods, an interface sets rules so that multiple classes follow identical rules.
Some of the enforced rules are:
- The name and number of methods.
- The types of the input and output parameters for the methods.
For now, we will not concern ourselves with making these interfaces, but with writing classes that follow the rules that they impose.
In the following part, we will show the interface structure only to show what the constraints are when implementing a class for it.
The following is the Animal interface:
/* File name : AnimalInt.java */ interface AnimalInt { public void eat(); public void travel(); public void sound(); public int numOfLegs(); }
Lets see what constraints will be enforced by the Animal interface:
- The names of the methods must be the same: eat, travel, sound, and numOfLegs.
- The types of the input and output parameters for the methods must be those indicated.
- All methods must be implemented!
The way to force the constraints of an interface on your class is by using the keyword implements and the name of the interface next to the class name. The effect of this is that, to make and use any instance of your class, you will have to follow the interface.
New Concept Alert!!
The notion of using instances of classes is new. We will go into detail about this subject at a later time. For now, the following things need to be clear:
- Classes are like templates or instruction manuals for making things and how they work.
- While you can use the Class by itself to do computation (as we've done so far), sometimes that is not enough.
- In a similar was as the types you've seen so far (int, double, boolean), Classes may be instantiated.
- We call an instance of a Class: an Object
- An object has the properties of a Class (the important ones for us right now are the methods).
- When using interfaces, we must use the objects!Object
This is the third type of programming structure/style we've seen.
The three types can be seen as different ways of cooking:
In the first version, one agent (you) does all the work. You buy just the ingredients for this order and you only learn how to perform every action once. Programming version: one single script with a series of instructions.
In the second version, you give the order to a cooking school/restaurant. They have different areas and staff that already know how to do every action. You just provide the order that activates every action. Programming version: One class with global variables and some methods.
In the third version, you hire graduates from the cooking school that know how to do everything the school can do (except teach other graduates). You can then give different orders to each graduate (cook) and they can end up making different things. Programming version: We use the class to create objects. Each object has potentially different variables and can perform the same actions (has the same methods).
The following is the Capybara implementation of the Animal interface:
What is a Capybara? Basically a giant hamster.
Instructions: You must create a file called Apple.java that implements the FruitInt interface.
/* File name : Capybara.java */
public class Capybara implements AnimalInt {
public void eat() {
System.out.println("Capybara chomp-chomps");
}
public void travel() {
System.out.println("Capybara wades");
}
public void sound() {
System.out.println("Wragh-wrooph-brumph");
}
public int numOfLegs() {
return 4;
}
public static void main(String args[]) {
// Object declaration
Capybara cap = new Capybara();
// Using the Object
cap.eat();
cap.travel();
cap.sound();
System.out.println( cap.numOfLegs() );
}
}
In-Class Exercise 1:
In the following exercise, you will implement an interface that we will provide for you and then complete each of the methods so that they perform a set of indicated actions.
/* File name : FruitInt.java */
interface FruitInt {
// returns the weight of the fruit in pounds
public double weight();
// returns the color of the fruit
// (1: red, 2: green, 3: yellow, 4: other)
public int color();
// prints a description of the taste of the fruit
public void taste();
}