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:
- If I were to make a Child object, I'd be invoking both the base's constructor and the child's.
- If a parent class was pointing at a derived class, the hidden method will be called (Usually hidden in the base class).
- 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.