Answer: By declaring base class constructor as Private we can avoid that class being inherited by another class. When we create the object for the derived class the base class constructor called automatically and executed. Hereit cannot be executed due to its protection level. See the code snippet:
namespace Example
{
class Base
{
private Base()
{
}
public void Test()
{
Console.WriteLine("Test");
}
}
class Program : Base
{
static void Main(string[] args)
{
Program pObj = new Program();
pObj.Test();
}
}
}
If we run the above code we get the following error :
'Example.Base.Base()' is inaccessible due to its protection level'
Asked In: Many Interviews |
Alert Moderator