Java Reference
In-Depth Information
Fish
, which inherits the
Animal
class. An object of the
Fish
class does not know how to walk. Because the
Fish
class is
inherited from the
Animal
class, it will also inherit the
walk()
method, which is the ability to
walk. Definitely, you will need to override the
walk()
method inside the
Fish
class. Now the
question arises, how should the
Fish
class implement the
walk()
method? Should it respond
by stating “I am a fish and I do not know how to walk.”? Should it throw an exception stating
“It is illegal to ask a fish to walk.”?
•
Suppose you go forward with this solution. You add a new class called
You can see that the third solution seems to be a very close solution. However, it is not an ideal one. It also proves
a point that inheritance is a good thing to use in Java programs, but it does not always provide an ideal solution.
An Ideal Solution
You are looking for a solution that should provide two things:
letThemWalk()
, in the
Walkables
class, should be able to send a “walk”
message (that is, invoke the
walk()
method) on all objects that are passed to it as its
parameter. This method should work with all types of objects that can walk (which you have
now or you will have in future).
•
A single method,
•
If you want to add the ability to walk to objects of an existing class, you should not be forced to
change the superclass of the class.
Interfaces in Java provide a perfect solution in this scenario. Before you start discussing interfaces in detail, let's
complete the solution to the problem presented in this section. First, you need to define an interface. For now, just
think of an interface as a programming contract.
An interface is declared using the keyword
interface
, which can have abstract method declarations. Note that
an abstract method does not have a body. Each interface should be given a name. Your interface is named
Walkable
. It
contains one method called
walk()
. The complete code for your
Walkable
interface is shown in Listing 17-1.
Listing 17-1.
The Declaration for a Walkable Interface
// Walkable.java
package com.jdojo.interfaces;
public interface Walkable {
void walk();
}
All classes whose objects can walk should implement the
Walkable
interface. A class can implement one or more
interfaces using the keyword
implements
in its declaration. By implementing an interface, a class guarantees that it
will provide an implementation for all methods declared in the interface or the class will declare itself abstract.
For now, let's ignore the second part and assume that the class implements all methods of the interfaces it
implements. If a class implements the
Walkable
interface, it must provide implementation for the
walk()
method.
Objects of the
Person
and
Duck
classes need the ability to walk. You need to implement the
Walkable
interface to
these classes. Listing 17-2 and Listing 17-3 have the complete revised code for these classes.
Listing 17-2.
The Revised Person Class, Which Implements the Walkable Interface
// Person.java
package com.jdojo.interfaces;
public class Person
implements Walkable
{
private String name;