Click to See Complete Forum and Search --> : Multiple Inheritance and conctructors


byteme
September 14th, 2009, 05:58 PM
I'm working on a project and I need some help with C# constructors. Let's say I have the following;


public class vehicle
{
public vehicle()
{
//do some initialization for all vehicles
}
}

public class automobile : vehicle
{
public auto : base
{
// do specific initialization for auto
}
}

public class truck : auto
{
public truck : base
{
// do specific initialization for truck
}
}

Now, I want truck to inherit from from automobile because there are methods that I want to reuse. However, I do not want to the call the auto constructor, I want to call the vehicle constructor instead.

Anyone know of a way I can do that?

Thanks in advance.

Frederick Lupien
September 15th, 2009, 08:00 AM
I am pretty sure that there are no supported way of doing this directly, but you can achieve it via a parametrized constructor in auto that will simply forward to the base class


public class automobile : someCommonClass
{
public auto : base
{
// do specific initialization for auto
}
public auto(bool bDummy) : base()
{
// do nothing
}
}

public class truck : someCommonClass
{
public truck : base(true)
{
// do specific initialization for truck
}
}




However, this is not very clean, I would suggest that if you share a part of the logic between auto and truck that they both inherit a common class with the given functionnality



public class vehicle
{
public vehicle()
{
//do some initialization for all vehicles
}
}

public class someCommonClass : public vehicle
{
// code common to both auto and truck
public someCommonClass : base()
{
// initialization common to both auto and truck
}
}
public class automobile : someCommonClass
{
public auto : base
{
// do specific initialization for auto
}
}

public class truck : someCommonClass
{
public truck : base
{
// do specific initialization for truck
}
}


Hope this helps.

monalin
September 15th, 2009, 10:13 AM
Why use a common class theres nothing wrong with inheritance here.

A truck is an automobile a automobile is a vehicle... straight down the inheritance line theres no problems here. Keep it like this. All you need to do to NOT call the constructor for the automobile class is to leave off the : base() from the constructor of the truck class.


public class vehicle
{
public vehicle()
{
//do some initialization for all vehicles
}
}

public class automobile : vehicle
{
public automobile() : base()
{
// do specific initialization for auto
}
}

public class truck : automobile
{
public truck()
{
// do specific initialization for truck
}
}

Frederick Lupien
September 15th, 2009, 11:41 AM
However, I do not want to the call the auto constructor, I want to call the vehicle constructor instead.


I thinks he wants to call vehicle constructor, hence my suggestion for two classes. As I said there are many ways to achieve this other than adding a new class, but not wanting to call the constructor of your base class could be a sign that along the way they will differ more and more.

What you need to evaluate is if truck will use 99% of the logic of automobile or just a subset of it. From there you can make the decision of adding a class or leaving your hierarchy as it is.