2

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.

Arun
  • 145
  • 1
  • 7
  • 4
    That all depends on the meaning of the word "corrupt" - and the design of the base class. But for example, if you have a type which is meant to be immutable, but isn't sealed, then anyone can create a mutable class derived from it - even if the state in the base class remains immutable, developers wouldn't be able to rely on the class itself being immutable. – Jon Skeet Mar 19 '15 at 10:17

3 Answers3

1

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.

Orace
  • 7,822
  • 30
  • 45
  • OMG, are you sre about your sample case? – Matías Fidemraizer Mar 19 '15 at 10:39
  • I will never try to forbid HIM to enter. – Orace Mar 19 '15 at 10:47
  • @Orace so what u r trying to say is, an object when marked as sealed cannot be changed in any way, there by it prevents any changes to the implementation provided in the base class since it cannot be inherited? – Arun Mar 19 '15 at 11:18
  • @Orace I get it now when we want the behavior of a class to be the same at all time we use sealed.sealed ensures that behavior is not changed Thank u so much!!! – Arun Mar 19 '15 at 11:31
0

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() {}
}
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

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.

Technovation
  • 397
  • 2
  • 12