0
class Food
{
    public Food()
    {
        Console.WriteLine("This is original food");
    }
    public void sayCheese(string s)
    {
        Console.WriteLine(s + " this is from the base");
    }
    public virtual void randomMethod()
    {
        Console.WriteLine("This is from a base class");
    }
}
class Meal : Food
{
    public Meal()
    {
        Console.WriteLine("This is an original meal");
    }
    public new void sayCheese(string s)
    {
        Console.WriteLine(s + " this is from the child");
    }
    public override void randomMethod()
    {
        Console.WriteLine("This is from a child class");
    }
}
class Program
{
    static void Main(string[] arg)
    {
        Food basePointbase = new Food(); //base class pointing at base object
        Food basePointChild = new Meal(); //base class pointing at child object
        Meal childPointChild = new Meal(); //child class pointing at child object
        basePointbase.sayCheese("basefrombase"); //will call the base's method
        basePointChild.sayCheese("basefromchild"); //will call the base's method
        childPointChild.sayCheese("childfromchild"); //will call the child's method
        basePointbase.randomMethod(); //will call base's method 
        basePointChild.randomMethod(); //will call child's method
        childPointChild.randomMethod(); //will call child's method
    }
}

Things I recognize:

  1. If I were to make a Child object, I'd be invoking both the base's constructor and the child's.
  2. If a parent class was pointing at a derived class, the hidden method will be called (Usually hidden in the base class).
  3. If a parent class was pointing at a derived class, the overridden method will be called (Usually overridden from the child class).

Yes, that is very clear. But it seems off. Isn't there more than just that? How does pointing work? What goes in the compiler when a parent points to a child and how does it make a difference rather than instantiating an object with the same class pointing at it?

Edit: I'm asking how the base class pointer would affect anything other than calling the hidden method. The question linked doesn't show those differences at all. Please read the question before marking it as a duplicate.

Techadox
  • 35
  • 3
  • There are dozens of similar questions. In particular you should have a look at the `new`-keyword which does hide the actual member only when using as your derived class. When casting to your base-class you surely execute the base-method, that is when using `MyBaseClass b = new Derived(); b.DoSomething()`. – MakePeaceGreatAgain Sep 06 '17 at 14:25
  • A "hidden method" is a non-virtual base class method hidden by a subclass method of the same name and prototype that has the `new` keyword. Then a call via a base reference calls the base method, and a call via a subclass reference calls the subclass method. What you're doing is overriding a virtual method, in which case the override in the actual runtime type gets called regardless of the declared type of the reference to the object. – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '17 at 14:26
  • https://dotnetfiddle.net/KvwFgF – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '17 at 14:32
  • Re the Edit: This is not a site for tutorials. Consult any C# newbe reference for how the basics work, and the provided link is just one of dozens on this site already about the finer details. – H H Sep 10 '17 at 21:24

0 Answers0