inheritance vs containment

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

it seems i've seen articles warning against excessive use of inheritance
"is a" vs "has a"
i think this is a sample of "is a" ... i don't know how i'd make it a "has
a" sample

i was thinking to try a simple inheritance like the following

public class Investment
{
public Investment()
{ }

public Investment(string name)
{
_name = name; //,<<every investment would have a name
}
public string Name { get; set; }
}

class Stock : Investment
{
public string Ticker { get; set; }//,< stocks have ticker symbol
}
class RealEstate : Investment
{
public string Address { get; set; }//<< houses have address
}

is that sort of how inheritance works?
ie save having to add multiple definitions of name in each subclass

and would deriving from investment like that affect xmlSerializing
a list of subtypes

other problems/advantages of this type of structure?

thanks
mark
 
mp said:
it seems i've seen articles warning against excessive use of inheritance
"is a" vs "has a"
i think this is a sample of "is a" ... i don't know how i'd make it a "has
a" sample

i was thinking to try a simple inheritance like the following

public class Investment
{
public Investment()
{ }

public Investment(string name)
{
_name = name; //,<<every investment would have a name
}
public string Name { get; set; }
}

class Stock : Investment
{
public string Ticker { get; set; }//,< stocks have ticker symbol
} []

other problems/advantages of this type of structure?

thanks
mark

well first problem is i can't create an instance :-)

i'm trying this
for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
{
name=saRet[rowCounter, 2].ToString ( );
ticker= saRet[rowCounter, 1].ToString ( );
Debug.Print ( "load stock {0},{1}", name, ticker );//<<<
this indicates i do have values

// A first chance exception of type
'System.InvalidCastException' occurred in Investments.exe
Stock stock = (Stock) new Investment ( name ); //<< throws
error


stock.Ticker =ticker ;
rtnList.Add ( stock );
}

i thought:
//wouldn't stock inherit the constructor?
public Investment(string name)
{
_name = name; //,<<every investment would have a name
}

no it doesn't apparently...
i changed to this...
public class Stock : Investment
{
public Stock ( string name ) : base ( name ) { }//<<<add ctor for
subtype??
public string Ticker { get; set; }
}
now this seems to work
Stock stock = new Stock(name);

is that the right way?
thanks
mark
 
i thought:
//wouldn't stock inherit the constructor?

no it doesn't apparently...
i changed to this...
public class Stock : Investment
{
public Stock ( string name ) : base ( name ) { }//<<<add ctor for
subtype??
public string Ticker { get; set; }
}
now this seems to work
Stock stock = new Stock(name);

is that the right way?

Yes, that is the right way.
 
it seems i've seen articles warning against excessive use of inheritance
"is a" vs "has a"
i think this is a sample of "is a" ... i don't know how i'd make it a "has
a" sample

Stock "is a" Investment.

Investment "has a" Name.

That is not a particular good example because it is
not so likely that people would have Investment inherit
from Name or String (the last is not even possible in .NET
because String is sealed).

But you would be surprised how often you see a developer
want to inherit from a completely unrelated class just
to get its methods.

public class Investment : MyFancyPersistingUtils

would be such an example.
i was thinking to try a simple inheritance like the following

public class Investment
{
public Investment()
{ }

public Investment(string name)
{
_name = name; //,<<every investment would have a name
}
public string Name { get; set; }
}

class Stock : Investment
{
public string Ticker { get; set; }//,< stocks have ticker symbol
}
class RealEstate : Investment
{
public string Address { get; set; }//<< houses have address
}

is that sort of how inheritance works?
Yes.

ie save having to add multiple definitions of name in each subclass

That is just a side effect.

The purpose of it is that you can treat Stock and RealEstate
as Investment.

F.ex. storing them in a List said:
and would deriving from investment like that affect xmlSerializing
a list of subtypes

I don't get that.
other problems/advantages of this type of structure?

See above.

Arne
 
mp said:
it seems i've seen articles warning against excessive use of inheritance
"is a" vs "has a"
i think this is a sample of "is a" ... i don't know how i'd make it a "has
a" sample

i was thinking to try a simple inheritance like the following

public class Investment
{
public Investment()
{ }

public Investment(string name)
{
_name = name; //,<<every investment would have a name
}
public string Name { get; set; }
}

class Stock : Investment
{
public string Ticker { get; set; }//,< stocks have ticker symbol
} []

other problems/advantages of this type of structure?

thanks
mark

well first problem is i can't create an instance :-)

i'm trying this
for (long rowCounter = 1; rowCounter<= iRows; rowCounter++)
{
name=saRet[rowCounter, 2].ToString ( );
ticker= saRet[rowCounter, 1].ToString ( );
Debug.Print ( "load stock {0},{1}", name, ticker );//<<<
this indicates i do have values

// A first chance exception of type
'System.InvalidCastException' occurred in Investments.exe
Stock stock = (Stock) new Investment ( name ); //<< throws
error


stock.Ticker =ticker ;
rtnList.Add ( stock );
}

i thought:
//wouldn't stock inherit the constructor?
public Investment(string name)
{
_name = name; //,<<every investment would have a name
}

no it doesn't apparently...
i changed to this...
public class Stock : Investment
{
public Stock ( string name ) : base ( name ) { }//<<<add ctor for
subtype??
public string Ticker { get; set; }
}

Correct.

Constuctors are not inherited so you need to call it.

I think the following would be typical:

public class Stock : Investment
{
public Stock ( string name, string ticker ) : base ( name )
{
_ticker = ticker;
}

now this seems to work
Stock stock = new Stock(name);

is that the right way?

It is a correct way.

Often you would want to do:

Investment inv = new Stock(name);

Arne
 
Back
Top