Reputation: 145
I am kind of new to c# and was studying about sealed class, when i came across this
'A sealed class is mostly used for security reasons by preventing unintended derivation by which the derived class may corrupt the implementation provided in the base class'
Is this really possible? can a derived class really corrupt base class's implementation? If so can someone please explain with an example.
Upvotes: 2
Views: 137
Reputation: 8359
Say you need some gate keepers:
public interface IGateKeeper
{
/// <summary>
/// Check if the given id is allowed to enter.
/// </summary>
/// <param name="id">id to check.</param>
/// <param name="age">age to check</param>
/// <returns>A value indicating whether the id is allowed to enter.</returns>
bool CanEnter(string id, int age);
... other deep needs ...
}
You may have a solid implementation of it to test the majority at the entrance of your bar:
public class MajorityGateKeeper : IGateKeeper
{
public virtual bool CanEnter(string id, int age)
{
return age >= 18;
}
... other deep implementation ...
}
And also have an implementation for the VIP room:
public class VipGateKeeper : MajorityGateKeeper
{
public override bool CanEnter(string id, int age)
{
// Do the majotity test and check if the id is VIP.
return base.CanEnter(id, age) && (id == "Chuck Norris");
}
}
And break it in a second:
public class DrunkGateKeeper : VipGateKeeper
{
public override bool CanEnter(string id, int age)
{
return true;
}
}
The DrunkGateKeeper is a VipGateKeeper so you can hide it's drunk (cast to VipGateKeeper). But it do a terrible job.
var gk = (VipGateKeeper) new DrunkGateKeeper();
var canEnter = gk.CanEnter("Miley Cyrus", 16); // true (sic)
If you make the VipGateKeeper sealed you are sure that it can't be drunk: an object of type VipGateKeeper is a VipGateKeeper nothing more.
Upvotes: 1
Reputation: 397
consider you defined and implemented a method in a class, in your program you need only this implementation. for example you return a message to user, whenever you call this method you expect that particular message be returned, consider you have a child class which overrides that method and shows another message, when you call that method you have another result which is not expected, if you mark a class as sealed, you prevent accidental inheritance. sealed is obverse side of design coin from abstract. when you define an abstract class, you allow classes to derive from it and implement their own implementations for methods but sealed class does not allow others to derive it at all.
Upvotes: 0
Reputation: 64943
You might corrupt base class implementations because of polymorphism.
If class A
has a virtual
method which can be overridable (i.e. polymorphic) and a class B
overrides the whole method, and B
doesn't output the same stuff as A
's implementation, then, it seems like B
has changed the actual behavior of A
's implementation.
sealed
in class level level (f.e. override sealed
) is meant to avoid both inheritance and polymorphism on the entire class.
You might also prevent polymorphism (inheritance is still possible) on certain members overriding them in a derived class and marking them with sealed
modifier:
public class A
{
public virtual void DoStuff() {}
}
public class B
{
public override sealed void DoStuff() {}
}
Upvotes: 0