1.

Can Derived Classes Have Greater Accessibility Than Their Base Types?

Answer»

No, Derived classes cannot have greater accessibility than their base types. For example the FOLLOWING code is illegal. using System;
internal class InternalBaseClass
{
public void Print()
{
Console.WriteLine("I am a Base Class Method");
}
}
public class PublicDerivedClass : InternalBaseClass
{
public static void Main()
{
Console.WriteLine("I am a Public Derived Class Method");
}
}

When you compile the above code an error will be generated stating "INCONSISTENT accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this SIMPLE, you cannot have a public class B that DERIVES from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

No, Derived classes cannot have greater accessibility than their base types. For example the following code is illegal. using System;
internal class InternalBaseClass
{
public void Print()
{
Console.WriteLine("I am a Base Class Method");
}
}
public class PublicDerivedClass : InternalBaseClass
{
public static void Main()
{
Console.WriteLine("I am a Public Derived Class Method");
}
}

When you compile the above code an error will be generated stating "Inconsistent accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this simple, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.



Discussion

No Comment Found