Initializing base portion of a derived class

J

J.J. Feminella

(Please disregard the previous message; I accidentally sent it before it was
completed.)

I have source code similar to the following.

public class Vehicle
{
protected string dataV;
// ... more protected fields
}

public class FourWheelVehicle : Vehicle
{
protected string dataFWV;
// ... more protected fields
}

public class SuvVehicle : FourWheelVehicle
{
protected string dataSUV;
// ... more protected fields
}

Here's my problem. The number of fields in each class is large; some are
value types and some are reference types. When I write constructors for
FourWheelVehicle and SuvVehicle, I'll be writing a lot of tedious copying,
for instance, something like:

// default constructor
public FourWheelVehicle()
{
this.dataV = "defaultV";
// initialization of other base class protected fields
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

It seems like it'd be a lot easier to write something like this:

// copy constructor for "base"-sliced part of derived class
public FourWheelVehicle(Vehicle v)
{
base = v.Clone(); // oops! illegal
this.dataFWV = "defaultFWV";
// initialization of other derived class protected fields
}

Even if I could do that, I'm not sure what to do about the further derived
classes, like SuvVehicle and further derived classes. Perhaps something
like:

public SuvVehicle(FourWheelVehicle fwv)
{
base = v.Clone(); // (still illegal)
this.dataSUV = "defaultSUV";
// initialization of other derived class protected fields
}

Might it be ultimately easier to aggregate all the fields into an instance
of a protected object in the class (e.g. VehicleData), or is this overkill?
This would let me do something like:

public FourWheelVehicle(Vehicle v)
{
this.VehicleData = v.VehicleData.Clone();
this.FourWheelVehicleData = new FourWheelVehicleData();
}

This isolates the initialization code to the Clone() method and the
constructor of the data-encapsulation class, rather than scattering it over
all the constructors. But the creation of an additional class solely to hold
data seems like it's just adding unnecessary complication/abstraction.

It seems like there should be a simple solution to this, but that's probably
because I haven't thought about it enough yet. I'd appreciate any input that
a more experienced developer could give. Thanks!

Sincerely,
JJ Feminella
Computer Science/Economics Undergraduate
University of Virginia | School of Engineering and Applied Science
 
M

Martin Robins

public class Vehicle {
protected string dataV;
public Vehicle() {
this.dataV = @"DataV";
}
}

public class FourWheelVehicle : Vehicle {
protected string dataFWV;
public FourWheelVehicle() : base() {
this.dataFWV = @"DataFWV";
}
}

public class SportsUtilityVehicle : FourWheelVehicle {
protected string dataSUV;
public SportsUtilityVehicle() : base() {
this.dataSUV = @"DataSUV";
}
}

within each instance of the SportsUtilityVehicle class, the following values
will be set
dataV = @"DataV"
dataFWV = @"DataFWV"
dataSUV = @"DataSUV"

You do not really need to add the " : base()" construct to do this either;
it simply helps readability - the default constructors for each class are
setting the values. As each class is instantiated, it creates an instance of
its base class using default constructors anyway.

HTH.


Martin.
 
J

J.J. Feminella

Martin,

Thanks for responding. I don't think my original question was very clear, I
got a little bogged down in providing examples of what I was trying to do.
What I wanted to do was to give myself a way, through constructors, of
instantiating multiple derived instances whose base portions were the same.
But I can't figure out a good way to slice off the base portion and factor
that out separately, then assign the appropriate derived class fields. It
seems like I have to make an entirely new set of assignments and some kind
of convoluted copy constructor to do that.

Also, my actual "Vehicle" and derived classes have many different fields. It
seems like it'd be tedious to write a lot of assignment statements, which is
why I wrestled with the idea of encapsulating all the data into an
aggregation class, e.g. VehicleData, etc., although this seems like an
unnecessary complication. This would certainly simplify assignment, though;
it'd only take a simple "this.VehicleData = new VehicleData();" to
instantiate and it'd keep my constructors tidy. I'd appreciate thoughts on
this route as well.

Best wishes,
JJ
 
P

Phil S

How about a "copy constructor" in the base class to initialize the base
fields? Then the derived class constructors could also have copy
constructors that call the base constructor.

Or do I understand that you're looking for some way to have multiple
instances "derive" from another instance? In that case, you could use
containment rather than subclassing to minimize storage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top