Making a Method Generic
Welcome to Interfaces
Interfaces are like blank classes with only abstract methods. In essence, an Interface is like a "contract" for a class, specifying the methods that a class implementing it must have. However, while a class can only subclass a single normal class, it can implement any number of interfaces. Any class that implements an Interface must implement all the methods of that Interface. Most of the time, Interfaces are named after the capabilities they enforce: Paintable, Sizable, Movable, Cookable, etc.
Back to our example, the PaintShop's paintCar(...) method doesn't really care that it takes in a Car, it only cares that the Car has a paint(...) method. Likewise, the same applies for the paintIncredibles(...) method. In fact, the only thing the paintSomething(...) method cares about is that the parameter has a paint(...) method. Interfaces are designed to solve this exact problem by specifying the methods a class must have.
Check Point 4
Write a
Paintableinterface that has apaint(...)method that takes in ajava.awt.Coloras the parameter. Review the lecture slides on Interfaces about how to write yourPaintableinterface.Note: Interfaces only provide the method signatures. Like classes, they have to be saved as a separate java file (ex:
Movableinterface has to be saved asMovable.java).- Implement the
PaintableInterface in yourCarandIncrediblesclasses. Since they both already define thepaint(...)method, you don't need to write one.
Now that the Car and Incredibles are Paintable, let's make the appropriate changes inside the PaintShop Class.
Check Point 5
- Comment out the
paintCar(...)method and thepaintIncredibles(...)methods from thePaintShopclass. Save! - Write a generic
paintObject(...)method in thePaintShopclass that takes in one parameter. What should the parameter's type be? How would you write the body of the method? Remember to Save! - Compile and run the app. It should behave the exact same way as last time.
Now that we have all this set up, let's really put it to use and see why that entire interface work paid off...
Check Point 6
- Edit the
Fish.javafile so that it implements thePaintableInterface. Save! - Try compiling the code. You should get an error that says the
Fishclass has not defined thepaint(...)method. Right! If you implement an interface, you must define all of its methods. - Write a
paint(...)method inFish.java. CallsetColor(...)on theFish's components. Save! - Create an instance of
FishinsideApp's constructor and tell thePaintShopabout theFish. Save! - Compile and run the application to see the results of your hard work.
You're done! No changes had to be made to the PaintShop; you don't need a paintFish(...) method.
Summary...
The purpose of this lab was to teach you how to implement polymorphism using Interfaces. The idea behind polymorphism is that instances of objects can be different types at the same time. Originally, the parameter passed to the PaintShop's paintCar(...) method was a Car. By adding the Interface, the object was not only of type Car, but also of type Paintable, a much more generic type that can be common for a lot of objects, such as cars, superheros, and marine animals. The Paintable Interface factors out painting commonality. This is a widely used design pattern to write more generic code, allowing the programmer to write one method instead of many.
A last few remarks about Interfaces...
Interfaces can also extend from other interfaces. For example, if you had a Scalar interface that defined a setMagnitude() and a getMagnitude() method, you could extend the Scalar interface to create a Vector interface. It could look as follows:
public interface Vector extends Scalar {
public void setDirection(Direction d);
public Direction getDirection();
}Anything that implemented the Vector interface would have to define all the methods in Scalar as well as all the methods in Vector.
Don't forget, a class can implement multiple interfaces. The Car class you wrote could also implement a Movable interface, as well as any other you deem necessary. You would separate each additional interface with a comma. The syntax to do that would be:
public class Car extends cs015.labs.lab3.Car implements Paintable, Movable {
//code
}Check Point 7
You are all done with lab 3! Grab a TA and get checked off. Yay!