Making inheritance less tedious
Sean Kelly
sean at f4.ca
Tue Feb 27 11:29:23 PST 2007
Kristian Kilpi wrote:
>
> I think class inheritance is a bit(?) tedious sometimes (that is,
> unnecessarily tedious).
> (I leave interfaces out of this discussion.) 'Problems', IMHO, with it are:
>
> 1) You cannot inherit constructors.
Consider:
module A;
class Pet
{
this( char[] name )
{
m_name = name;
}
private char[] m_name;
}
module B;
import A;
class Puppy : Pet {}
class Aibo : Pet {} /* arguably a pet */
Assuming the ctor is inherited, then so far so good. Puppies and Aibos
both have names. Now let's say the base class has some new
functionality added:
class Pet
{
this( char[] name, Food eats )
{
m_name = name;
}
private char[] m_name;
private Food m_eats;
}
The new inherited behavior makes sense for Puppies, but not necessarily
for Aibos. One might argue that a refactoring of the hierarchy is a
good idea here, or that Aibos aren't actually pets, but I think it's
reasonable to assume that because inheritance suggests specialization,
it may occasionally be desirable to place constraints on inherited
attributes. Forcing the programmer to implement ctors for a class is a
one simple way to avoid subtly breaking behavior from changes up the
inheritance tree.
Sean
More information about the Digitalmars-d
mailing list