2

I have the following classes:

namespace ConsoleApplication8
{
    public abstract class Employee
    {
        public virtual void Show()
        {
            Console.WriteLine("from base.");
        }
    }

    public class Manager:Employee 
    {
        public void Show()
        {

            Console.WriteLine("from child.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var man=new Manager();
            man.Show();
            Console.ReadKey();
        }
    }
}

Here I haven't use any override keyword for the method Show() in the derived class Manager but the code is running ok. So what is the actual use of override keyword.

user3801163
  • 105
  • 1
  • 1
  • 7
  • try Employee man = new Manager(); and Manager man = new Manager(); with and without override instead of var man = new Manager(); and see if you still have a question. – Pete Baughman Apr 02 '15 at 06:46
  • The key point here is how the variable defined in *compile-time*. When it is already referenced as `Manager` (using `var` in your case), the compiler knows to call `Manager.Show`. *Polymorphism* comes into play when you refer to `man` as the base type (`Employee`), now the `virtual` keyword tells the compiler to check for possible overridden methods in *runtime* (typically using a vTable http://en.wikipedia.org/wiki/Virtual_method_table). – haim770 Apr 02 '15 at 06:53

3 Answers3

11

You're not overriding the original method, you're hiding it. This is also possible, but maybe not what you want or what you'd expect, as you lose polymorphism. I'll give an example:

static void Main(string[] args)
{
    var man=new Manager();
    man.Show();
    Console.ReadKey();
}

This is you code and outputs "From child". The following, when using "override" would also output "From child". In your case it will not.

static void Main(string[] args)
{
    var man=new Manager();
    (man as Employee).Show();
    Console.ReadKey();
}

A similar question was asked here: virtual keyword in c#. While it doesn't address the override keyword, the problem class is about the same.


Real life example of when polymorphism is useful. I had to write an application to calculate the bonuses for all employees in a company based on their roles. As you did, I had an employee class with a public virtual double CalculateBonus(); method and several classed derived from that.

Thanks to Polymorphism, all I had to do is iterate the List<Employee> of all employees, regardless of their roles, and call the CalculateBonus method, as polymorphism made sure that always the overrides were called.

Without override, the bonuses would all have been 0, as that was the default result for CalculateBonus in the class Employee.

Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • @haim770 - he's using [hiding](https://msdn.microsoft.com/en-us/library/aa691135%28v=vs.71%29.aspx) in a technical sense. – Damien_The_Unbeliever Apr 02 '15 at 06:47
  • Pssh to implementation/subtype overriding. Interfaces (and explicit implementations) forever! – user2864740 Apr 02 '15 at 07:06
  • @user2864740 And re-implement shared functionality in every class...? – Thorsten Dittmar Apr 02 '15 at 07:35
  • @ThorstenDittmar SRP and DI. Although I'm not going to say 'never useful' (and a UI library is a fairly good counter example due to the inherent tree structure), I believe that most code should have a very low subtype-to-component ratio, keeping it for special cases (and school lectures). General dislike for implementaiton subtyping aside, I think this is a fairly good answer for the scope of the question - but the 'other' side of the coin (interfaces) and how such fit in with [virtual] methods and polymorhpism is often eschewed. – user2864740 Apr 02 '15 at 16:17
2

https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx https://msdn.microsoft.com/en-us/library/ms173153.aspx

you would be hiding the underlaying method so you may get a compiler warning about it and use the "new" keyword in conjunction to hide the underlaying member. override is simple - you "override" the underlaying implementation detail with your own implementation

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
0

Check the difference

void Main()
{
    Person instance1=new SpecificPerson();
    instance1.Execute();//Person

    SpecificPerson instance2=new SpecificPerson();
    instance2.Execute();//Specific
}

    public class Person
    {
        public virtual void Execute()
        {
            Console.WriteLine("Person");
        }
    }

    public class SpecificPerson:Person
    {
        public void Execute()
        {
            Console.WriteLine("Specific");
        }
    }

Overriding vs method hiding

Community
  • 1
  • 1
Artiom
  • 7,694
  • 3
  • 38
  • 45