Java Reference
In-Depth Information
You may notice that there is a similarity between the Person and Duck classes. An object of both classes can
respond to a “walk” message as both of them provide a walk() method. However, the similarity between the two classes
ends right there. They are not linked in any other ways at all, except for the fact that both of them have the Object
class as their common ancestor. The introduction of the Duck class has expanded the walking ability of objects in your
application. Before there were ducks, only people could walk. After you add the Duck class, ducks can walk as well.
Now, you want to let ducks walk using your Walkables class. Can your Walkables class let the ducks walk? No.
It cannot let the ducks walk unless you make some changes. The ability of a Duck to walk does not pose any problem
for the existing Walkables class. The problem at this point is that the letThemWalk() method has declared its
parameter type as an array of Person. A Duck is not a Person . You cannot write code as shown below. A Duck object
cannot be assigned to a reference variable of the type Person . The following snippet of code will not compile:
Person[] list = new Person[3];
persons[0] = new Person("Jack");
persons[1] = new Duck("Jeff"); // A compile-time error
persons[2] = new Person("John");
Walkables.letThemWalk(list);
How do you solve this problem so your Walkables class will let a person and a duck walk together? There are
three ways to solve this problem with your existing knowledge of the Java programming language. Note that we are not
talking about interfaces at this point. You will solve this problem efficiently and correctly using interfaces at the end
of this section. Let's just forget about the title of this chapter for now, so you can appreciate the important role that an
interface plays in the Java programming. The three ways to solve this problem can be listed as follows:
Change the parameter type of the
letThemWalk() method of the Walkables class from an array
of Person to an array of Object . Use reflection to invoke the walk() method on all elements of
the passed-in array. Do not worry about the term “reflection” at this point. This is covered in
Chapter 3 book of Beginning Java Language Features (Apress, 2014).
Define a new
static method called letDucksWalk(Duck[] ducks) in the Walkables class.
Call this method when you want ducks to walk.
Person and Duck classes from a common ancestor class, say Animal class, and add a
walk() method in the Animal class. Change the parameter type of the letThemWalk() method
of the Walkables class from an array of Person to an array of Animal .
Let's look at each of the three solutions in detail.
Inherit the
Proposed Solution #1
You can implement the first solution by adding the two methods to the Walkables class, as shown:
// Walkables.java
import java.lang.reflect.Method;
public class Walkables {
public static void letThemWalk(Object[] list) {
for (int i = 0; i < list.length; i++) {
// Get the walk method reference
Method walkMethod = getWalkMethod(list[i]);
 
Search WWH ::




Custom Search