Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relationship between subclasses 1

Status
Not open for further replies.

maiago

Programmer
Dec 10, 2005
7
IT
Hi.
A question about relationship between subclasses of two hierarchies.
If I have two hierarchies, where root classes are, for example: Car and Driver. Subclasses are CarType1,CarType2 and DriverType1,DriverType2.

I want to model a relation such as "drive" or "is driven by", but I want to say two things:
1) each Driver---drive---a Car;
2) DriverType1 have to drive CarType1, DriverType2 have to drive CarType2.

Well. I have put a relation between parents Driver and Car in order to satisfied 1).
The second constraint (2) have to be exspressed with a relation between DriverType1(or 2) and CarType1(or 2)??
Is it reasonable in an object oriented design??
In what way such a system will be used with polymorphism??
 
Each Car has a Driver.
So in the Car base class, I would create a private pointer to a Driver, and add Set/Get methods to set or get the current driver...
If DriverType1 can ONLY driver CarType1... then you should make the Get/Set methods abstract and implement them in the right way for each derived class.
 
yeah.

Or instead of set/get for the driver just do

car.drive(driver);
 
Ok. In this way I will have a method in Car such as setDriver:)Driver).
I just think about this solution.
But in subclasses CarType1 and CarType2 I will have to put a method with the same signature, and I would like instead methods like setDriver:)DriverType1) and setDriver:)DriverType2) but in this way I don't overwrite method in the base class...
 
you have to override to get your type safety.
otherwise you'd have two setDriver methods on the base class. One taking type1 and one taking type2. At that point it would be pointless. What's your aversion to overriding?
 
And so class CarType1 like:

public class CarType1 extends Car {

private DriverType1 driver;

setDriver (Driver _driver) {
driver=(DriverType1)_driver;
}

getDriver (Driver _driver) ...

}

Simply I'd like to avoid the downcast.
 
Why is it necessary to only take a specific derived Driver type for each Car class? Couldn't you do something like this (I mostly use C++, so the code might not be 100% valid C# or Java):
Code:
public class Car
{
   public abstract bool SetDriver( Driver  driver );

   public Driver GetDriver() const
   {
      return m_Driver;
   }

   protected Driver  m_Driver;
}

public class CarType1 extends Car
{
   public bool SetDriver( Driver  driver )
   {
      if ( typeof( driver ) == DriverType1 )
      {
         m_Driver = driver;
         return true;
      }

      return false;
   }
}
If you like to use lots of exceptions, you could even throw an exception instead of returning false...
 
Ok, I just want to understand if there was a way to avoid downcasting or "if" statements, in order to ensure the right use of the system just with the Java compiler.
Thank's for the tips!
Bye
 
use an interface (driver), implementation (DriverImpl) and a subclas of the implementation (Driver1/Driver2). and pass in a type of interface Driver1/Driver2

Car can be looking for Driver1 as a parameter but can talk to is using the driver interface.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top