Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusion regarding overriding rules C#

I have a litte confusion regarding method overriding and the validity of OOP priciples. I know everything regarding sealing, shadowing, overriding, virtual etc. but I came across a scenario, that just confused me. Suppose I have:

class classA
{

    public virtual void sayhello()

      {
        Console.WriteLine("hello I'm A");
    }

};

class classB :classA
{
    public override void sayhello()
    {
        Console.WriteLine("hello I'm B");
    }

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

        classB a = new classB();
        a.sayhello();
    }
}

According to everything I studied so far, a method declared as virtual or abstract (in abstract class) can be overriden using override keyword in child class. according to this, above code works perfect. When I remove the virtual keyword, and then try to override the method using override keyword, then compiler gives error as:

cannot override inherited member 'inheritence.classA.sayhello()' because it is not marked virtual, abstract, or override

and then i removed the override key word, from child class, and provided the implementation as:

class classB :classA
{
    public void sayhello()
    {
        Console.WriteLine("hello I'm B");
    }

};

In this case, the method could be overrided. I'm able to override the method, which is not virtual or abstract. so, my question is:

1. Did it not violate the OOP principle? since I'm able to override the method, which is not marked as virtual in parent.

2. Why am I allowed to override the method this way? which is not even marked virtual?

3. Removing virtual keyword from classA method, it gave me the feeling of sealed method in classA, when I tried to override that method in classB. (as I mentioned the compiler error earlier). If i remove virtual, so that the child class may not override it, then WHY could the child class cleverly override it, removing its override keyword? Is this ONLY the case, sealed keyword is designed for?

like image 841
Zeeshan Avatar asked Dec 21 '13 09:12

Zeeshan


1 Answers

I will like to tell you that you gave hidden the parent child method not overridden.
One more thing you might have not noted doing the same is seeing WARNING because in warning section it will be clearly mentioned that,

Warning 'line number' 'classB .sayhello' hides inherited member 'classA.sayhello'. Use the new keyword if hiding was intended.

Your question,

Did it not violate the OOP principle? since I'm able to override the method, which is not marked as virtual in parent.

No surely it did not violate the OOP principle as you have hide the base class method.

Why am I allowed to override the method this way? which is not even marked virtual?

Because C# not only supports overriding but also method hiding and A hiding method has to be declared using the new keyword. For More Information read dotnet_polymorphism and overriding-vs-method-hiding

Is this ONLY the case, sealed keyword is designed for?

From MSDN sealed sealed keyword is designed to prevent derivation of class and to negate the virtual aspect of the virtual members.

  • When applied to a class, the sealed modifier prevents other classes from inheriting from it.
  • sealed modifier can be only applied to a method or property that overrides a virtual method or property in a base class. This prevent further overriding specific virtual methods or properties but it can never stop method-hiding. Read Non-overridable method for more information
like image 142
dbw Avatar answered Sep 30 '22 10:09

dbw