In an online C# tutorial [1] there is the following statement:
"The override modifier allows a method to override the virtual method of its base class at run-time. The override will happen only if the class is referenced through a base class reference."

I tested this with the following code:

using System;

class Program {
    static void Main(string[] args) {

        Father p1 = new Son();
        Son p2 = new Son();

        p1.print();
        p2.print();

        Console.ReadLine();
    }
}

class Father {
    public virtual void print() {
        Console.WriteLine("Father");
    }
}

class Son : Father {
    public override void print() {
        Console.WriteLine("Son");
    }
}

However, I get this result:

Son
Son

It looks to me that p1 is in fact being referenced as the base class, and should therefore not be overridden. Am I misunderstanding? Is the tutorial wrong? I looked in MSDN [2] and it seems to confirm:
"At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed."


[1] http://www.csharp-station.com/Tutorials/Lesson09.aspx
[2] http://msdn.microsoft.com/en-us/library/ms173152.aspx

Instead of override, use "new" in front of the method.

It's awkwardly worded. When you call the print method on the p2 object, you technically are calling that classes method. When you call it on the p1 object, you are 'overriding' the base class method with the derived classes method.

Instead of override, use "new" in front of the method.

I'm not trying to make the code run a specific way. I am trying to understand the term "base class reference". Regarding this code:

Father p1 = new Son();

Is not Son referenced from the base class?

It's awkwardly worded. When you call the print method on the p2 object, you technically are calling that classes method. When you call it on the p1 object, you are 'overriding' the base class method with the derived classes method.

Thank you for the explanation. If p1 is not a "base class reference" then what is a base class reference?

Thanks.

AS Momerath stated, its an awkward example.
But you can use a base keyword to access members of the base class from within a derived class:

class Program
    {
        static void Main(string[] args)
        {
            Son p1 = new Son();
            Father p2 = (Father)p1;
            Father p3 = new Father();

            p1.print();
            p2.print();
            p3.print();
            Console.ReadLine();
        }
    }

    class Father
    {
        public virtual void print()
        {
            Console.WriteLine("Father");
        }
    }

    class Son : Father
    {
        public override void print()
        {
            base.print();
            Console.WriteLine("Son");
        }
    }

or you can use constructors, with base method to access to base constuctor:

class Program
    {
        static void Main(string[] args)
        {
            Son p1 = new Son();
            Father p2 = (Father)p1;
            Father p3 = new Father();
            Console.ReadLine();
        }
    }

    class Father
    {
        public Father()
        {
            Console.WriteLine("Father`s constructor");
        }
    }

    class Son : Father
    {
        public Son()
            : base()
        {
            Console.WriteLine("Son`s constructor");
        }
    }

Or this was (this is what you wanted) using a new keyword on the method in Son`s class:

class Program
    {
        static void Main(string[] args)
        {
            Son p1 = new Son();
            p1.print();

            Father p2 = new Son();
            p2.print();
            
            //Hidden base class members can still be accessed from client code 
            //by casting the instance of the derived class to an instance of the base class.
            Father p3 = (Father)p1;
            p3.print();           

            Console.ReadLine();
        }
    }

    class Father
    {
        public virtual void print()
        {
            Console.WriteLine("Father");
        }
    }

    class Son : Father
    {
        public new void print()
        {
            Console.WriteLine("Son");
        }
    }

Thank you for the explanation. If p1 is not a "base class reference" then what is a base class reference?

That is why I said it is awkwardly worded. For the purpose of this example, p1 is a base class reference.

Technically, p1 is a variable of the type of the base class with a reference to a derived class.

Thanks, Mitja, but I seem to not understand the phrase "base class reference" considering its awkward phrasing. So I'll ignore it and ask what I think it means. Considering the example in the OP, presented here to reduce scrolling:

class Father {
    public virtual void print() {
        Console.WriteLine("Father");
    }
}
 
class Son : Father {
    public override void print() {
        Console.WriteLine("Son");
    }
}

If one were to create an object of type Son, how could he access Father's print method? A cast is not a solution as it will hide other essential Son methods.

I don't have a use case, rather I am simply trying to learn polymorphism and experimenting in code gives me the opposite answers to what I expect when reading MSDN. Actually, the Visual Studio 2005 version of the Polymorphism page explains it better in my opinion:
http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.80%29.aspx

I'll go through that and start testing code. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.