I'm pretty confused between some concepts of OOP: virtual
, override
, new
and sealed override
. Can anyone explain the differences?
I am pretty clear that if the derived class method is to be used, one can use the override
keyword so that the base class method will be overriden by derived class. But I'm not sure about new
, and sealed override
.
The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.
The new modifier is used to define ShowDetails in the ConvertibleCar class. The override modifier is used to define ShowDetails in the Minivan class.
The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
The difference between override and new is that override extend the method of base class with new definition but new hides the method of base class.
The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.
This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:
public class Base { public virtual void SomeMethod() { } } public class Derived : Base { public override void SomeMethod() { } } ... Base d = new Derived(); d.SomeMethod();
will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.
Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:
public class Base { public virtual void SomeOtherMethod() { } } public class Derived : Base { public new void SomeOtherMethod() { } } ... Base b = new Derived(); Derived d = new Derived(); b.SomeOtherMethod(); d.SomeOtherMethod();
Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.
If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).
An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derived class from further overriding the property. The accessors of a sealed property are also sealed.
Any method can be overridable (=virtual
) or not. The decision is made by the one who defines the method:
class Person { // this one is not overridable (not virtual) public String GetPersonType() { return "person"; } // this one is overridable (virtual) public virtual String GetName() { return "generic name"; } }
Now you can override those methods that are overridable:
class Friend : Person { public Friend() : this("generic name") { } public Friend(String name) { this._name = name; } // override Person.GetName: public override String GetName() { return _name; } }
But you can't override the GetPersonType
method because it's not virtual.
Let's create two instances of those classes:
Person person = new Person(); Friend friend = new Friend("Onotole");
When non-virtual method GetPersonType
is called by Friend
instance it's actually Person.GetPersonType
that is called:
Console.WriteLine(friend.GetPersonType()); // "person"
When virtual method GetName
is called by Friend
instance it's Friend.GetName
that is called:
Console.WriteLine(friend.GetName()); // "Onotole"
When virtual method GetName
is called by Person
instance it's Person.GetName
that is called:
Console.WriteLine(person.GetName()); // "generic name"
When non-virtual method is called the method body is not looked up - compiler already knows the actual method that needs to be called. Whereas with virtual methods compiler can't be sure which one to call, and it is looked up at runtime in the class hierarchy from down to up starting at the type of instance that the method is called on: for friend.GetName
it looks starting at Friend
class and finds it right away, for person.GetName
class it starts at Person
and finds it there.
Sometimes you make a subclass, override a virtual method and you don't want any more overrides down in the hierarchy - you use sealed override
for that (saying you are the last one who overrides the method):
class Mike : Friend { public sealed override String GetName() { return "Mike"; } }
But sometimes your friend Mike decides to change his gender and thus his name to Alice :) You could either change original code or instead subclass Mike:
class Alice : Mike { public new String GetName() { return "Alice"; } }
Here you create a completely different method with the same name (now you have two). Which method and when is called? It depends on how you call it:
Alice alice = new Alice(); Console.WriteLine(alice.GetName()); // the new method is called, printing "Alice" Console.WriteLine(((Mike)alice).GetName()); // the method hidden by new is called, printing "Mike"
When you call it from Alice
's perspective you call Alice.GetName
, when from Mike
's - you call Mike.GetName
. No runtime lookup is made here - as both methods are non-virtual.
You can always create new
methods - whether the methods you are hiding are virtual or not.
This applies to properties and events too - they are represented as methods underneath.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With