Better still to stay with OOP : source is attached
public interface Vehicle
{
  public void setSpeed(int speed);
}

abstract public class Car implements Vehicle
{
 public Boat(){}
}

abstract public class Boat implements Vehicle
{
 public Boat(){}
}

public class LandTransport extends Car
{
        String speed = "60";
        public LandTransport(){}
        public void setSpeed(int speed) {
          try {
                this.speed = Integer.toString(speed);
        } catch(NumberFormatException e) {}
        }
        public String toString() {
          return "The car is going "+speed+"/mph";
        }
}

public class WaterTransport extends Boat
{
        String speed = "30";
        public WaterTransport(){};
        public void setSpeed(int speed) {
          try {
                this.speed = Integer.toString(speed);
        } catch(NumberFormatException e) {}
        }
        public String toString() {
          return "The boat is going "+speed+"/knots";
        }
}

import java.util.ArrayList;

public class Transports {
        LandTransport car = new LandTransport();
        WaterTransport boat = new WaterTransport();
        ArrayList trans = new ArrayList(2);
        public Transports() {
                car.setSpeed(60);
                boat.setSpeed(30);
                trans.add(car);
                trans.add(boat);
        }
        public void drive() {
                for(int i=0; i<trans.size(); i++) {
                  Vechicle vh = (Vechicle)array.get(i);
                  System.out.println(vh.toString());
                }
        }
        public static void main(String [] args) {
                Transports tr = new Transports();
                tr.drive();
        }
}

-----Original Message-----
From: Madhav Vodnala [mailto:[EMAIL PROTECTED]]
Sent: Saturday, May 25, 2002 1:11 AM
To: JDJList
Subject: [jdjlist] RE: Same Method, Multiple Interfaces


Its the intent of the author. He just expects the boat/car to have a
'driving facility'. If he really wanted to have totally different 'drive()'.
then probably he would have enforced 'driveBoat()' in Boat and driveCar() in
Car class.

----- Original Message -----
From: "Jason Kilgrow" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Saturday, May 25, 2002 2:08 AM
Subject: [jdjlist] RE: Same Method, Multiple Interfaces


> 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. 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?
>
> "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


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

Attachment: Boat.java
Description: Binary data

Attachment: Car.java
Description: Binary data

Attachment: LandTransport.java
Description: Binary data

Attachment: Transports.java
Description: Binary data

Attachment: Vehicle.java
Description: Binary data

Attachment: WaterTransport.java
Description: Binary data

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

Reply via email to