Re: Design problem with inheritance



Responding to Srinivasarao_moturu...

class ABC
{
public :
virtual void operation1();
virtual void operation2();
virtual int GetValue();
virtual char GetValue();
virtual void SetValue(int);
virtual void SetValue(char);
}

class intABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(int);
int GetValue();
private:
int val;
}

class charABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(char);
char GetValue();
private :
char val;
}

The problem here is that intABC does not provide implementations for the char versions of GetValue and SetValue while charABC does not provide implementations for the int versions of GetValue and SetValue. If they are derived from ABC, they /must/ provide implementations for /all/ properties identified in ABC, regardless of LSP. One manifestation of that construction violation happens to be that they are not substitutable in an LSP sense.

If GetValue and SetValue are defined in ABC, then the semantics of the data domain for 'val' must always be a numeric value because that is the only way they can be substitutable for all members. At the 3GL level method overloading provides a convenient way around the problem of representation. But then one does not need the subclasses; one only needs ABC. One chooses an int or char internal representation and does the conversion to the other representation in the relevant getter/setter.

If some members of ABC can have a 'val' data domain that includes non-numeric values, then one cannot define GetValue and SetValue in ABC because they cannot be overloaded properly for all members. One must define them only in intABC and charABC as unique specializations for that subclass. IOW, exactly as you have done but without identifying them in ABC.


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH



.



Relevant Pages