1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Prevent Inheritance

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

  1. arunlalds

    arunlalds Banned

    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