Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Sealed Classes And Methods
WhatsApp
Jasbeer Singh
8y
9.4
k
0
3
25
Blog
SealedClasses.zip
Sealed keyword enables us to prevent the inheritance of the class or the class members that were virtual.
Sealed Class
A class can be declared sealed by putting keyword sealed before the class definition.
Public sealed
class
MyClass {}
Sealed class can never be a base class, so it can never be an abstract class. It cannot be inherited by any other class. If we put a sealed keyword before any method, Indexer, property, event or class then it will negate its virtual acceptance for any further derived class.
The last class in the hierarchy is the sealed class. Sealed class cannot be a base class nor can it be abstract class.
The virtual nature of a method persists for any number of levels of inheritance. e.g
A class “A” contains a virtual method “VM”. Another Class “B” is inherited from A and one more class “C” is inherited from B. In this type of situation, class “C” can override the method “VM” regardless of whether class “B” overrides the Method “VM”. Now, if at any level of inheritance, we want to restrict next derived class from inheriting the virtual method “VM”, then we will create the method, using the keyword sealed.
Public sealed override
int
sum(
int
a,
int
b) {}
Let’s understand by C# example
In the example given below, we have a base class as a normal class and derived class as NormalDerived. If we inherit NormalDerived from normal class, there will be no problem executing the program.
class
Program {
//Normal Class
public
class
normalClass {
public
void
Display() {
Console.WriteLine(
"This is a normal class which can be further inherited"
);
}
}
//Normal Derived
public
class
NormalDerived normalClass {
// this Derived class can;t inherit BaseClass because it is sealed
}
static
void
Main(string[] args) {
normalClass obj =
new
normalClass();
obj.Display();
Console.ReadLine();
}
}
Output will be
Now, if we see same for the sealed class, we will get the error. For sealed class, we have written a program, as shown below.
class
Program {
//Normal Class
//Sealed Class
public
sealed
class
BaseClass {
public
void
Display() {
Console.WriteLine(
"This is a sealed class which can;t be further inherited"
);
}
}
//Tried to derive Sealed
public
class
SealedDerived BaseClass {
// this Derived class can;t inherit BaseClass because it is sealed
}
static
void
Main(string[] args) {
BaseClass obj =
new
BaseClass();
obj.Display();
Console.ReadLine();
}
}
Now, we have tried to inherit from sealed class but we can’t inherit from sealed class, when we build it, it will throw an exception.
Error has occurred during the running of the program and the class cannot inherit from sealed class.
Sealed Method
Sealed method is used to control the overriding nature of the virtual method. Sealed method can be defined by using sealed keyword before return type of method.
The example is given below.
public
override sealed
void
MyFunc() {
Console.WriteLine(
"I am a Sealed method"
);
}
Sealed method cannot be overridden
in
a
class
class
SealedMethod {
public
class
BaseClass {
public
void
myFunc() {
Console.WriteLine(
"This is a normal method"
);
}
}
//Tried to derive Sealed
public
class
SealedDerivedM BaseClass {
// this sealed method will now not be overrideen
public
virtual sealed
void
myFunc() {
Console.WriteLine(
"This is a SealedDerived method which can't be further inherited"
);
}
}
public
class
threeDerived SealedDerivedM {
public
virtual
void
myFunc() {
Console.WriteLine(
"This is a sealed class which can't be further inherited"
);
}
}
static
void
Main(string[] args) {
BaseClass obj =
new
BaseClass();
obj.myFunc();
Console.ReadLine();
}
}
Class threeDerived is inheriting from class SealedDerivedM but the method myFunc is overriding sealed in SealedDerivedM class, so when we run this program, it will give us an error. Let us run this program and see the output, if it gives any.
It has thrown a sealed method error that it can’t be overridden. Sealed keyword is used before a method to stop it from being overridden in the derived class.
Please provide your valuable feedback.
Sealed Classes
C#
Up Next
When To Use Abstract Class/Methods Instead Of Normal Class/Methods In C#
Ebook Download
View all
LINQ Quick Reference with C#
Read by 44.7k people
Download Now!
Learn
View all
Membership not found