hi doug, glad you did take it as constructive critisism ;o) I do think it does make sense to take such examples just as far as necessary to exclude misunderstandings. I think we both agree that this example could be driven *way* further... but then it would just go beyond the scope of these discussions. cheers to all
> what was a simple example to show who was driving has turned into a > complete > design session. This is very good and taking it one step beyond just a > simple example to a more complete OOD is what these sessions are all > about, > thanks for taking the time some of us don't have. > > doug... > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Sunday, May 26, 2002 10:00 PM > To: JDJList > Subject: [jdjlist] RE: Same Method, Multiple Interfaces > > > > Better still to stay with OOP : source is attached > > (...) > > I do not think this is a good approach. > I would recommend the following instead: > > 1. "Vehicle" should be a base class (perhaps with the method > setSpeed(...) (and similar methods common to all "Vehicle"s) declared > 'abstract') > and not an interface, because there most likely will be some attributes > common > to all Vehicle-classes (e.g. maximum speed, current speed etc.) > > 2. LandTransport and WaterTransport are clearly less specialized than cars > and boats > (e.g. trains are also LandTransports...) and therefore can not be derived > >from the more > special classes Car and Boat. > therefore, it is better to declare LandTransport and WaterTransport (and > consequentially > AirTransport) as interfaces (some perhaps empty, so-called "marker > interfaces") to enforce these types > for classes derived from "Vehicle". > Moreover, with your concept you can not define classes that describe > vehicles that are e.g. of type LandTransport AND WaterTransport > (hoovercrafts or amphibian > cars and the like) because you would have to extend both the "Car" and > "Boat" > classes, which is impossible in JAVA (single inheritance is one of the > reasons why interfaces > are a part of JAVA) > on the other hand such classes may implement more than one "...Transport" > (marker-)interface. > > public abstract class Vehicle > { > // add here attributes common to all vehicles, e.g. > float speed; > .... > // add here methods common to all vehicles in functionality, e.g. > public void setSpeed(float speed) > { > this.speed = speed; > } > > // add here methods common to all vehicles that > // can only be implemented in the derived classes, e.g. > // this method has to be implemented individually for each > // derived class, because it is different to put fuel into > // a car or into a nuclear powered submarine.... > // we would have to create a Fuel-class as well which > // works as base class for all types of fuel... > > public abstract void putFuel(Fuel f); > > // REMARK: if you expect to derive classes that describe > // vehicles without any kind of fuel-driven motor (sailing boats, > // rowing boats, bicycles, ....) create an interface > // MotorDriven which enforces the method: > // public interface MotorDriven > // { > // public abstract void putFuel(Fuel f); > // } > // only some classes will implement this interface. the idea is > // the same as for AirTransport (see example below) > // this concept can be used for all kinds of properties that > // would require multiple inheritance otherwise, e.g. > // > // class Car extends Vehicle implements > LandTransport,IndividualTransport,MotorDriven > // > // class JumboJet extends Vehicle implements > AirTransport,MassTransport,MotorDriven > // > // class Clipper extends Vehicle implements > WaterTransport,MassTransport,WindDriven > // (btw, a clipper is a big sailing ship ;-) > } > > public interface LandTransport { } // "marker interface" > public interface WaterTransport { } // "marker interface" > public interface AirTransport > { > // add here methods common to all air vehicles that are > // enforced inside the derived classes, e.g. > public void setMaxFlightHeight(float height); > } > > public class Car extends Vehicle implements LandTransport > { > public putFuel(Fuel f) > { > // put gasoline inside.... > } > } > > // for objects of type "AirTransport" one can be sure of the existance > // of the setMaxFlightHeight() method (because it is enforced by the > interface) > > public class Airplane extends Vehicle implements AirTransport > { > float maxHeight; > public void setMaxFlightHeight(float maxHeight) > { > this.maxHeight = maxHeight; > } > public putFuel(Fuel f) > { > // put kerosine inside... > } > } > > public class Hoovercraft extends Vehicle implements LandTransport, > WaterTransport > { > public putFuel(Fuel f) > { > // put inside whatever hoovercrafts are powered with ;-) > } > } > > all objects can be treated as type "Vehicle" (and stored in an array for > instance) > and can be tested for Land-,Water-,AirTransport type > e.g. > > Vehicle v = new Airplane(); > > if (v instanceof AirTransport) > v.setMaxFlightHeight(10000); // method sure to be there and > implemented > > v.putFuel(new Fuel(...)); // method sure to be there and implemented > ... > Vehicle h = new Hoovercraft(); > > if (h instanceof LandTransport) .... // true! > if (h instanceof WaterTransport) ..... // true! > ... > Vehicle x = new Vehicle(); // ERROR, abstract class, not fully > implemented, cannot be instantiated > > > > > 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 > > > > -- > GMX - Die Kommunikationsplattform im Internet. > http://www.gmx.net > > > 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 > -- GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net To change your membership options, refer to: http://www.sys-con.com/java/list.cfm
