Hiding an inherited method/property

  • Thread starter Thread starter A Ratcliffe
  • Start date Start date
A

A Ratcliffe

Back again...

I have some classes that inherit from each other, as an example...

Point (abstract base class)

Point3 (for 3D points), derived from Point

Point4 (adds a homogenous co-ordinate) derived from Point3

Point3 has a Method - MoveBy(float dx, float dy, float dz), for moving it
relatively in 3D space.

Point4 has its own equivalent - MoveBy(float dx, float dy, float dz, float
dw), but I want to hide Point3's MoveBy, so it isn't available when you are
working with a Point4. There are other methods/properties of Point3 that
still make sense for Point4, so I don't want to love all the inherited
methods/properties.

Hope this makes sense, and thanks for all your help,

Yours,

Ann-Marie Ratcliffe
 
A Ratcliffe said:
I have some classes that inherit from each other, as an example...

Point (abstract base class)

Point3 (for 3D points), derived from Point

Point4 (adds a homogenous co-ordinate) derived from Point3

Point3 has a Method - MoveBy(float dx, float dy, float dz), for moving it
relatively in 3D space.

Point4 has its own equivalent - MoveBy(float dx, float dy, float dz, float
dw), but I want to hide Point3's MoveBy, so it isn't available when you are
working with a Point4. There are other methods/properties of Point3 that
still make sense for Point4, so I don't want to love all the inherited
methods/properties.

It sounds like your Point4 shouldn't be derived from a Point3 - it
breaks Liskov's Substitutability Principle (namely that whatever you
can do with a Point3, you should be able to do with a Point4).
 
It sounds like your Point4 shouldn't be derived from a Point3 - it
breaks Liskov's Substitutability Principle (namely that whatever you
can do with a Point3, you should be able to do with a Point4).

Looks like you are right on that basis. Actually, these are 'helper'
functions for the individual classes. Everything else works correctly, but
these are just meant to provide a quicker way of moving/setting the points
than individually setting the co-ords.

On that basis, they could be removed and I wouldn't have this problem. But
Helper functions/methods are developer-friendly...

Thanks for your help,

Ann-Marie
 
If it makes sense, you might make a MoveBy(dx, dy,dz) in Point 4 that
calls the 4 argument version with dw=0. That way Point4 is logically related
to Point3. [Perhaps that makes no sense in your case, but it's worth
considering]
Bob
 
Mon, 3 Nov 2003 12:46:13 -0000, on
microsoft.public.dotnet.languages.csharp, A Ratcliffe wrote:

[...]
Point3 has a Method - MoveBy(float dx, float dy, float dz), for moving it
relatively in 3D space.

Point4 has its own equivalent - MoveBy(float dx, float dy, float dz, float
dw), but I want to hide Point3's MoveBy, so it isn't available when you are
working with a Point4. There are other methods/properties of Point3 that
still make sense for Point4, so I don't want to love all the inherited
methods/properties.
[...]

Hi
If I've understood correctly, the "new" keyword should be the cure for your
problem :)
In Point4 you should define method MoveBy starting with "new" keyword.
Have you tried this?

Greetz
Piotr Olech
 
Back
Top