Jason Kilgrow wrote:

>public interface Car
>{
>    public void drive();
>}
>
>public interface Boat
>{
>    public void drive();
>}
>
>public class Amphibious
>    implements Car, Boat
>{
>    public void drive()
>    {
>        System.out.println("I'm driving my amphibious");
>    }
>}
>
>
>Which interface's method have I implemented? Does it matter?
>
It doesn't, afaik, matter to Java.  But it shows a shortcoming in your 
OO design.  Your ambiguity comes because you have two "things", a car 
and a boat, that are conceptually related but you don't relate them in 
the design.  A better design (though still not necessarily the best) 
would be to have a base interface, say Vehicle, that has the drive 
method and the two sub interfaces, Car and Boat, both of which extend 
it.  Then you could create an amphibious car without trying to make it 
into a boat.

As a general rule, whenever you find yourself with two or more classes 
or interfaces that have an area of commonality, or "overlap", place the 
common features into a super class or interface, whichever is 
appropriate, and remove them from the separate classes or interfaces. 
 This will solve most, if not all, problems with ambiguity.

public interface Vehicle {
    // Define actions/characteristics common to all vehicles
    public void drive();
}
public interface Car extends Vehicle {
    // Define actions/characteristics unique to cars
}
public interface Boat extends Vehicle {
    // Define actions/characteristics unique to boats
}
public class Amphibious implements Car {
    public void Drive() {
        ...
    }
}


Tomm



To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to