Advertisement
//C# Example of "public" class members
using System;
class A
{
//A public instance variable of class A
public int i = 10;
//A public method of class A
public void A_Message()
{
Console.WriteLine("Hello from class A");
}
}
class B
{
public static void Main(String[] ar)
{
//Creating an object of class A
A ob= new A();
//Calling a method of class A by using its object
ob.A_Message();
//Accessing the value of instance variable of an object of class A
Console.WriteLine(ob.i);
}
}
Hello from class A
10
//C# Example of an incorrect "private" class members
using System;
class A
{
//A private instance variable of class A
private int i = 10;
//A private method of class A
private void A_Message()
{
Console.WriteLine("Hello from class A");
}
}
class B
{
public static void Main(String[] ar)
{
//Creating an object of class A
A ob= new A();
//Calling a method of class A by using its object
ob.A_Message();
//Accessing the value of instance variable of an object of class A
Console.WriteLine(ob.i);
}
}
Example2.cs(26,5): error CS0122: 'A.A_Message()' is inaccessible due to its protection level
Example2.cs(29,23): error CS0122: 'A.i' is inaccessible due to its protection level
//C# Example of a correct "private" class members
using System;
class A
{
//A private instance variable of class A
private int i = 10;
//A private method of class A
private void A_Message()
{
Console.WriteLine("Hello from class A");
}
}
//Defining the Main() method of class A
public static void Main(String[] ar)
{
//Creating an object of class A
A ob= new A();
//Calling a method of class A from within its own class
ob.A_Message();
//Accessing the value of instance variable from within its own class
Console.WriteLine(ob.i);
}
}
Hello from class A
10
Advertisement
//C# Example of an incorrect access of "protected" class members
using System;
class A
{
//Protected instance variable of class A
protected int i = 10;
//Protected method of class A
protected void A_Message()
{
Console.WriteLine("Hello from class A");
}
}
class B
{
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Accessing a protected method of class A
//outside its class
ob.A_Message();
//Accessing the instance variable, i of class A
//Outside its class
Console.WriteLine(ob.i);
}
}
Example2.cs(27,5): error CS0122: 'A.A_Message()' is inaccessible due to its protection level
Example2.cs(31,23): error CS0122: 'A.i' is inaccessible due to its protection level
//C# Example of correctly accessing the "protected" class members
using System;
class A
{
//Protected instance variable of class A
protected int i = 10;
//Protected method of class A
protected void A_Message()
{
Console.WriteLine("Hello from class A");
}
}
//Class B inheriting from class A
class B : A
{
public static void Main(String[] ar)
{
//Creating an object of class B
B ob = new B();
//Accessing a protected method of class A by inheritance
//Because, it is inherited by class B
ob.A_Message();
//Accessing the instance variable, i of class A, by inheritance
Console.WriteLine(ob.i);
}
}
Hello from class A
10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement