Search code examples
c#fieldderived-class

Specify field type in derived class


I have a Rectangle2d and a Rectangle3d deriving from it for backward compatibility reasons. In doing so, the type of the class' "transform" field shall change from Transform2d to Transform3d which is a derived version of Transform2d. Here's a simplified example:

class Transform2
{
    protected float positionX;
    protected float positionY;
}

class Transform3 : Transform2
{
    protected float positionZ;
}


class Rectangle2d
{
    protected Transform2 transform;
}

class Rectangle3d : Rectangle2d
{
    // Does not work: Just hides Rectangle2d.transform
    protected new Transform3 transform;
}

One solution I do not prefer is going without a Transform class and direct fields instead:

class Rectangle2d
{
    protected float positionX;
    protected float positionY;
}

class Rectangle3d : Rectangle2d
{
    protected float positionZ;
}

In my eyes, when the second method works and the first method is just the second method with some bunching, there should be a clean solution for it. At least it hope so.

version: .NET Framework 4.6.1


Solution

  • Inheritance implies an is-a relationship. Rectangle3d is not 2d, so it should probably not inherit from Rectangle2d

    That said, If you must do this I suggest you use generics

    public abstract class Transform<T>
    {
        protected T transform;
    }
    
    public class Rectangle2d : Transform<Transform2> {}
    public class Rectangle3d : Transform<Transform3> {}
    

    In this model both 2d and 3d have a property transform which is strongly typed.