Search code examples
c#classinheritanceradix

C# Class Inheritance


greetings. i have the following class:

public class Ship
{
    public enum ValidShips
    {
        Minesweeper,
        Cruiser,
        Destroyer,
        Submarine,
        AircraftCarrier
    }

    public enum ShipOrientation
    {
        North,
        East,
        South,
        West
    }

    public enum ShipStatus
    {
        Floating,
        Destroyed
    }

    public ValidShips shipType { get; set; }

    public ShipUnit[] shipUnit { get; set; }

    public ShipOrientation shipOrientation { get; set; }

    public ShipStatus shipStatus { get; set; }

    public Ship(ValidShips ShipType, int unitLength)
    {
        shipStatus = ShipStatus.Floating;

        shipType = ShipType;

        shipUnit = new ShipUnit[unitLength];

        for (int i = 0; i < unitLength; i++)
        {
            shipUnit[i] = new ShipUnit();
        }
    }
}

i would like to inherit this class like so:

public class ShipPlacementArray : Ship
{

}

this makes sense.

what i would like to know is how do i remove certain functionality of the base class?

for example:

    public ShipUnit[] shipUnit { get; set; } // from base class

i would like it to be:

    public ShipUnit[][] shipUnit { get; set; } // in the derived class

my question is how do i implement the code that hides the base class shipUnit completely?

otherwise i will end up with two shipUnit implementation in the derived class.

thank you for your time.

ShipPlacementArray deals with only one ship. but the array reflects the directions the ship can be placed at.


Solution

  • In my experience, every time I've wanted to remove a property from a class in an inherited class, my class design was flawed. One way to solve this problem would be to create a new base class without the ShipUnit property and then inherit that base class two times, once for your concrete Ship type and once for your concrete ShipPlacementArray type. In both subclasses you could implement ShipUnit as you need to.