Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
iTEgg Avatar asked Feb 22 '10 14:02

iTEgg


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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

You don't. This is not the point of inheritance. The point of inheritance is to inherit the base class functionality -- all of it -- and adding/changing stuff. It's for "is-a" relations, e.g., "a car is a vehicle."

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

It sounds like you want ShipPlacementArray to be a wrapper, or container, of multiple Ship objects. This does not seem like a case where inheritance should be used.

Ask yourself the question: "is a ShipPlacementArray a kind of Ship?"

like image 119
Thomas Avatar answered Nov 17 '22 00:11

Thomas