C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: In class B, we can access the protected int, but not the private int. So protected gives us additional access in a derived class.
Note: The accessibility domain of protected fields includes all derived classes. This is a key difference.
C# program that uses protected modifier
using System;
class A
{
protected int _a;
private int _b;
}
class B : A
{
public B()
{
// Can access protected int but not private int!
Console.WriteLine(this._a);
}
}
class Program
{
static void Main()
{
B b = new B();
}
}
Output
0
Example: We see an internal "Test" class (just for this example). Any member type can also be modified with internal.
Tip: You can have internal fields, properties, or methods. This program can instantiate the Test type because it is in the same program.
C# program that uses internal modifier
class Program
{
static void Main()
{
// Can access the internal type in this program.
Test test = new Test();
test._a = 1;
}
}
// Example of internal type.
internal class Test
{
public int _a;
}
So: In other words, no external program will be able to access the internal type.
Note: Accessibility in the C# language determines what regions of program text are allowed to access a specific member.
Thus: A C# program that is contained in a different binary file (such as DLL or EXE) will not successfully access the internal member.
Maintenance: In large programs, maintenance is a huge issue. Information hiding (helped with internal) helps here.
Tip: In most programs, internal is not needed. For larger, more complex programs, it becomes more useful.
Details: Protected internal means both internal and protected. The "internal" means only the current program can use the member.
However: With protected internal, derived classes in other programs can use the member. The modifier does not prevent this.
Thus: Protected internal is less restrictive than just protected. And it is not as common in programs.