C# · CodeProject · Dotnet

Ending Virtual overriding tree in Inheritance

Hello Amigos,

Today i stumbled upon on a problem where in one has to stop the virtual overriding hierarchy tree in inheritance. I see in many cases it is needed to safe guard your hierarchy tree in your application so that no other Evil class does inheritance to your subclass and over rides it again.

I am sure not many of you guys will not be using this in your daily code base,  hence it so happens that we tend to forget this good feature provided to us.

Hence i am going to outline about it :

  1.     class MyBase
  2.     {
  3.         public virtual void Hello() { }
  4.     }
  5.     class MyDervied : MyBase
  6.     {
  7.         public sealed override void Hello()
  8.         {
  9.         }
  10.     }
  11.     class MyOtherClass : MyDervied
  12.     {
  13.         public override void Hello() //Error.
  14.         {
  15.         }
  16.     }

In the above code, you can’t keep overriding the virtual methods to nth level. Hence to make your code safe and secure, you could stop/end the overriding tree.

Thanks 🙂


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s