A non-inheritable class and method

Introduction

It’s a common question during interview why we should create non inheritable class. It’s answer also simple. We don’t want to make it inheritable.

Details

If you want to make a class non inheritable then you have to use sealed keyword in the C# class as follows.

public sealed class Log
{

}

For VB.NET you have to use NotInheritable keyword.

Public NotInheritable Class Log
 
End Class

For non inheritable method of a class you have to use same keyword.

public class Log
{
    protected sealed void Save()
    {

    }
}
Public Class Log
    Protected NotOverridable Sub Save()

    End Sub
End Class

Last word

We can restrict methods from overriding further using sealed or NotInheritable keyword in the respective languages.

Comments

Email
Print