Public, Private, Protected at Class Level
Whenever we create a class, we always want to have the scope to decide who can access certain members of the class. In other words, we would sometimes need to restrict access to the class members. The one thumb rule is that members of a class can freely access each other. A method in one class can always access another method of the same class without any restrictions. When we talk about the default behavior, the same class is allowed complete access but no else is provided access to the members of the class. The default access modifier is
private
for class members.
Point to remember: The default access modifier is
private
for class members.
Let’s do some hands on lab. Just open your Visual Studio and add a console application in C# named
AccessModifiers
. You’ll get a Program.cs class file by default. In the same file, add a new class namedModifiers
and add the following code to it:
Hide Copy Code
using System;
namespace AccessModifiers
{
class Modifiers
{
static void AAA()
{
Console.WriteLine("Modifiers AAA");
}
public static void BBB()
{
Console.WriteLine("Modifiers BBB");
AAA();
}
}
class Program
{
static void Main(string[] args)
{
Modifiers.BBB();
}
}
}
So, your Program.cs file becomes like shown in the above code snippet. We added a class
Modifiers
and twostatic
methods AAA
and BBB
. Method BBB
is marked as public
. We call the method BBB
from Main
method.The method is called directly by the class name because it is marked static
.
When we run the application, we get the output as follows:
Output
Hide Copy Code
Modifiers BBB
Modifiers AAA
BBB
is marked public
and so anyone is allowed to call and run it. Method AAA
is not marked with any access modifier which automatically makes it private
, that is the default. The private
modifier has no effect on members of the same class and so method BBB
is allowed to call method AAA
. Now this concept is called member access.
Modify the
Program
class and try to access AAA
as:
Hide Copy Code
class Program
{
static void Main(string[] args)
{
Modifiers.AAA();
Console.ReadKey();
}
}
Output
Hide Copy Code
'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
So , since method
AAA
is private
, therefore no one else can have access to it except Modifiers
class.
Note: Each and every code snippet written in this article is tried and tested.
Modifiers
Now mark the
AAA
method as protected, our class looks like:
Hide Copy Code
class Modifiers
{
protected static void AAA()
{
Console.WriteLine("Modifiers AAA");
}
public static void BBB()
{
Console.WriteLine("Modifiers BBB");
AAA();
}
}
Program
Hide Copy Code
class Program
{
static void Main(string[] args)
{
Modifiers.AAA();
Console.ReadKey();
}
}
Output
Hide Copy Code
'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
Again the same output. We cannot access the method
AAA
even after we introduced a new modifier namedprotected
. But BBB
can access AAA
method because it lies in the same class.
No comments:
Post a Comment