C# · CodeProject · Dotnet

Stop overriding tree growth in Inheritance

Hi Reader,

Recently i saw a question where in it says “How you can stop from subclass overriding your methods in the inheritance hierarchy”.

The solution was simple but did not flashed into my brains. By using sealed keyword in the method signature along with override, you can stop the overriding tree growth.

Let me show you an example:

Now lets see internally what the compiler trick is:

Actually the compiler uses Final keyword internally for sealing the method. Hence the tree growth stops here. Note here that, the Final keyword is not an replacement for Sealed keyword just like Family for Protected access modifier.

If you use the same Sealed keyword for a class, the compiler does not replaces it with Final. So Final keyword is only specific to sealing a method and not a class.

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