> Very true. And I expect that if I were to use instanceof on an object of
> type Amphibious, it would test true for both Car and Boat types. 

very true ;o)

> However, was that the intent of the author of Car and Boat? Does it
matter?
> Clearly, from the name of the interface, the drive() method of Car was
> meant to be something totally different than that of Boat. Syntactically,
> Amphibious satisfies the requirements of both Car and Boat. But does it
satisfy
> the logical requirements?

I would suggest that if you do have a set of such interfaces
that denote some properties of objects (and are on the same "logical" level)
and you expect some objects to have more than one property at a time,
restrict yourself to "marker interfaces", i.e. empty interfaces with no
method
declarations (have a look at e.g. java.io.Serializable).
such interfaces only add a new type to the object that implements them and
you can (as stated above) test for these types with the
"instanceof"-operator.
the drive()-method would best be declared in a common base class (e.g.
"Vehicle")
(perhaps as an abstract method, which forces all subclasses to implement the
method.
additionally, the class has to be declared abstract as well, which means it
can not be
instantiated. this is good, since "Vehicle" is a general base calss and not
suitable for
object instantiation)
greets
w.

e.g.:
(remark: gave the marker interfaces some more general names...)

public abstract class Vehicle    //class must be declared abstract, because
it contains abstract method
{
public abstract void drive();
...
}

public interface WaterVehicle { }          // marker interface

public interface LandVehicle { }           // marker interface

public class Boat extends Vehicle implements WaterVehicle
{
public void drive()   { ...... }            // must be implemented!
}

public class Car extends Vehicle implements LandVehicle
{
public void drive()   { ...... }           // must be implemented!
}

public class Amphibious extends Vehicle implements LandVehicle,WaterVehicle
{
public void drive()   { ...... }          // must be implemented!
}

-------------

Amphibious x = new Amphibious();
(x instanceof LandVehicle)  yields true
(x instanceof WaterVehicle)  yields true

ps: btw, in C++ this would undoubtedly be implemented with multiple
inheritance.....
long live interfaces ;o)
 
> "Fourt, Martha" <[EMAIL PROTECTED]> wrote:
> 
> >Your drive() method in Amphibious implements the drive() method in both
> Car
> >and Boat. �You could cast an Amphibous object to an object reference of
> type
> >Car, or to one of type Boat. �In either case, you could call its drive()
> >method.
> >
> > � �Car aCar = (Car) (new Amphibious());
> > � �Boat aBoat = (Boat) (new Amphibious());
> > � �aCar.drive();
> > � �aBoat.drive();
> >
> >
> >
> >-----Original Message-----
> >From: Jason Kilgrow [mailto:[EMAIL PROTECTED]]
> >Sent: Friday, May 24, 2002 3:07 PM
> >To: JDJList
> >Subject: [jdjlist] Same Method, Multiple Interfaces
> >
> >
> >Group,
> >Consider the following:
> >
> >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?
> >
> >
> >__________________________________________________________________
> >Your favorite stores, helpful shopping tools and great gift ideas.
> >Experience the convenience of buying online with Shop@Netscape!
> >http://shopnow.netscape.com/
> >
> >Get your own FREE, personal Netscape Mail account today at
> >http://webmail.netscape.com/
> >
> >
> >To change your membership options, refer to:
> >http://www.sys-con.com/java/list.cfm
> >
> >To change your membership options, refer to:
> >http://www.sys-con.com/java/list.cfm
> >
> 
> 
> __________________________________________________________________
> Your favorite stores, helpful shopping tools and great gift ideas.
> Experience the convenience of buying online with Shop@Netscape!
> http://shopnow.netscape.com/
> 
> Get your own FREE, personal Netscape Mail account today at
> http://webmail.netscape.com/
> 
> 
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm
> 

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net


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

Reply via email to