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

  1. Write a Paintable interface that has a paint(...) method that takes in a java.awt.Color as the parameter. Review the lecture slides on Interfaces about how to write your Paintable interface.

    Note: Interfaces only provide the method signatures. Like classes, they have to be saved as a separate java file (ex: Movable interface has to be saved as Movable.java).

  2. Implement the Paintable Interface in your Car and Incredibles classes. Since they both already define the paint(...) 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

  1. Comment out the paintCar(...) method and the paintIncredibles(...) methods from the PaintShop class. Save!
  2. Write a generic paintObject(...) method in the PaintShop class that takes in one parameter. What should the parameter's type be? How would you write the body of the method? Remember to Save!
  3. 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

  1. Edit the Fish.java file so that it implements the Paintable Interface. Save!
  2. Try compiling the code. You should get an error that says the Fish class has not defined the paint(...) method. Right! If you implement an interface, you must define all of its methods.
  3. Write a paint(...) method in Fish.java. Call setColor(...) on the Fish's components. Save!
  4. Create an instance of Fish inside App's constructor and tell the PaintShop about the Fish. Save!
  5. 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:

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:

Check Point 7

You are all done with lab 3! Grab a TA and get checked off. Yay!