Search code examples
c#classinheritanceoverridingsubclass

C# overriding a method with different parameters


I am working on an assignment that involves inheritance and overriding methods. I am supposed to create a Ship class and a CruiseShip subclass. The ship class is supposed to have two member variables, a string for the ship's name and a string for the ships build date. The class also needs a Print method to display that information.

The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers. The subclass is supposed to override the Ship class's Print method and display only the ship name and capacity (not the date).

The issue with my code below is that I am not sure how to override the Print method to take a string and an integer, instead of two strings. How can I have the override method display the appropriate date?

public class Ship
{
    public virtual void Print(string name, string date)
    {
        Console.WriteLine("The ship's name: {0}", name);
        Console.WriteLine("The ship's build date: {0}", date);
    }
}

public class CruiseShip : Ship
{
    public override void Print(string name, int capacity)
    {
        Console.WriteLine("The ship's name: {0}", name);
        Console.WriteLine("The ship's passanger capacity: {0}", capacity);
    }
}

public class SeeShips
{
    static void Main(string[] args)
    {
        Ship newShip = new Ship();
        newShip.Print("Generic Ship","1989");
        Console.ReadLine();
    }
}

Solution

  • The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.

    Imagine I create one of your ships:

    var myShip = new Ship();
    

    Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:

    public class Ship
    {
        //these should be properties or
        //readonly variables but thats for later
        public string name; 
        public string date;
    }
    

    Ok, now you can create a Ship and give it some state:

    var myShip = new Ship();
    myShip.name = "My Ship";
    myShip.date = "Yesterday";
    

    Great! Now we do have an interesting ship in our hands; it has a name and a date.

    Furthermore, now the virtual method Print doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:

    public class Ship
    {
         ....
         public virtual string Print() {
            Console.WriteLine("The ship's name: {0}", name);
            Console.WriteLine("The ship's build date: {0}", date); }
    

    You see how that works?

    From here, you should be able to figure out how to subclass CruiseShip and override Print and make it do what you want.

    Subjects you will definitely want to read about:

    1. Constructors and initializing state
    2. Why properties instead of public member fields
    3. Read only member fields and properties and immutable types