Reputation: 3024
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?
Upvotes: 5
Views: 1033
Reputation: 6276
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.
method-hiding
. Read Non-overridable method for more informationUpvotes: 3
Reputation: 11098
What you did is method hiding (as others already explained).
If you really want to do it you should add new keyword in method definition to make warning disappear and also as a documentation. So that other developers looking at your code know that you did it on purpose.
Upvotes: 1
Reputation: 11717
Well, it's quite simple in the end:
new
keyword (or leaving it out completely), you're doing a static replacement operation at compile time based on the type information that is available in your code. These are two totally different things.
Upvotes: 1
Reputation: 24895
I think this comes from C++ implementation, which used slicing (What is object slicing?).
While C# resembles mostly Java, in some cases (this one and the existence of value types) it follows the C++ way.
The reasoning after this is that, since your code calls method sayhello
from an A
variable, the programmer expects the logic of A.sayhello
to be executed. It does not break OOP principles, since you are executing an A.sayhello
implementation (so it must match the A.sayhello
contract).
The difference with Java is not OOP vs not OOP, but that Java uses late binding
(the actual method to be executed is decided at runtime based in a
actual instance) while C# uses early binding
(the method is decided at compile time) unless the method is virtual.
Personally I prefer late binding, while C# approach is correct from an OOP point of view, I find that usually the more specialized method should be used.
Upvotes: 1
Reputation: 26209
- Did it not violate the OOP principle? since I'm able to override the method, which is not marked as virtual in parent.
You did not override
the parent method
you have hidden the parent method.
so you can never access the parent method from the child class object as it was hidden
by your child class method sayhello()
.
2.Why am I allowed to override the method this way? which is not even marked virtual?
because you can hide the parent methods with child implemetation.
Upvotes: 1