How do you prevent a class from overriding in .NET?

You can prevent a class from being overridden. This is possible by using a keyword. In C# you will be using the keyword sealed to prevent overriding and in VB.NET you will be using the keyword NotInheritable to achieve the same. Here is an example to demonstrate both of them:



Prevention of Class Overriding in C# Using sealed Keyword:
Usage of sealed keyword is demonstrated in the example shown below:
public sealed class sealedClass {
public void displayMsg() {
Console.WriteLine(“In Sealed Class…”);
}
}
class derivedClass : sealedClass {
public displayMsg() {
Console.WriteLine(“In Derived Class…”);
}

}
class testClass {
public static void Main() {
derivedClass obj = new derivedClass();
}
}

In this example, you have defined the class sealedClass as sealed. You try to inherit sealedClass using the derivedClass and define/override the method displayMsg of sealedClass. You have also defined the class testClass. Then you have created an instance of derivedClass inside Main method of testClass. Now try compiling this code, you will end up in the following error:
“derivedClass cannot inherit from sealed class sealedClass.”

In this example, you have used sealed keyword on a class and ensured that the class cannot be overridden or inherited. C# also provides a provision to prevent overriding of a specific method in the class by specifying the sealed keyword along with the method signature instead of specifying sealed keyword along with the class name.

Prevention of Class Overriding in VB.NET Using NotInheritable Keyword:
Consider the following example in which you have a sealed class called sampleClass and you are going to access this class and its method by creating an instance of it:

Module sampleModule
sub Main()
Dim sampleObj As New SampleClass()
sampleObj.displayMsg()
End Sub
End Module

Public NotInheritable Class sampleClass
Public Sub displayMsg()
Console.WriteLine(“Inside displayMsg of sampleClass”)
End Sub
End Class

This example will work fine and it will print the following output in the console:
Inside displayMsg of sampleClass

Problem arises when you try to inherit this class sampleClass which is marked as NotInheritable. Consider that the following class is added into your program:
Public class derivedClass
Inherits sampleClass
Public Overrides Sub displayMsg()
Console.WriteLine(“Inside displayMsg of derivedClass”)
End Sub
End Class

Now your program will not compile and you will end up in the following error:
derivedClass cannot inherit from class sampleClass because sampleClass is declared NotInheritable

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.