1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Prevent Inheritance

Discussion in 'C#' started by arunlalds, Mar 13, 2010.

  1. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
    Mark the class as sealed.
    Code:
    sealed class MyClass
    {
    ...
    }
    
    Structs are inherently sealed
    Prevent Overriding of a Single Method
    Put sealed as part of the method or property definition.
    Code:
    class ParentClass
    {
    public virtual void MyFunc() { }
    }
    class ChildClass : ParentClass
    {
    //seal base class function into this class
    public sealed override void MyFunc() { }
    }
    class GrandChildClass : ChildClass
    {
    //yields compile error
    public override void MyFunc() { }
    }
    
     

Share This Page