Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - ACCESS MODIFIERS


Access Modifiers provides certain level of restriction to the usage of the class members so that they are not accidentally modified.


Let us understand it in detail.


The main motto of using Classes and Objects is to maintain certain standards. So, that the data inside the objects do not get modified or deleted accidentally.


i.e. Let us consider the class Human.


Example :



class Human {

	int age;
	
	public int Age 
	{
		get { return age; } 
    	set { age = value; }
	}
}



The above class Human has an attribute called age.


And we have created a property Name and placed the getter and setter in it.


public int Age
{
	get { return age; }
	set { age = value; }
}

So, we do not access the data members (i.e. age) directly from outside the class. We need to use a property called Name and a getter and setter method to do that.


And this is where Access Modifiers comes into picture. It puts a restriction on the variables.


There are 4 types of Access Modifiers in C# :

  1. Private

  2. Public

  3. Protected

  4. Internal

Let us see each in detail :


Private Access Modifier


A Private access modifier will never allow a class member to be accessed outside the class.Usually, an attribute is marked as private(i.e. age in the above class). So, that they should not be accessed outside the class.


A private data member can be accessed only inside a class.


Example :



class Human {

	private int age;
	
	public int Age 
	{
		get { return age; } 
    	set { age = value; }
	}
}



Public Access Modifier


A Public data member can be accessed outside the class. Usually, methods in a class are Public and attributes in a class are private.


Also, a class can be declared public, so that this class is accessible and visible to all the classes in all the packages in C#.


But if a public class you are trying to access, is in a different package. You need to import that class in your package.


If you are declaring an attribute of a class public. You can access it from outside the class. No getter and setter method is needed for that. But that's never a good practice.


class Human {

	public int age; // Not a good practice but allowed.
}

Protected Access Modifier


A Protected data member can be accessed within the same class and in the class which is inherited from that class.


Example :



class LivingBeing 
{
    protected string source;
}

class Human : LivingBeing 
{
	
	public Human()
	{

	}

	public Human(string sourceTemp)
	{
		source = sourceTemp;
	}
    
    public void breathe() 
    {
    	System.Console.WriteLine("Breathes oxygen from "+source);
    }
}

class CreateHuman
{
	public static void Main(string[] args)
	{
        Human human1 = new Human("Air");

        human1.breathe();
	}
}  


Output :



  Breathes oxygen from Air

So, in the above example, we have defined a class LivingBeing, that only has an attribute source which is marked as protected.


class LivingBeing
{
	protected string source;
}

Then we have defined a class named Human and inherited the LivingBeing class.


class Human : LivingBeing

Now, there is a breathe() method in Human class,


public void breathe()
{
	System.Console.WriteLine("Breathes oxygen from "+source);
}

That uses the property of the LivingBeing class, i.e. source.


class LivingBeing
{
	protected string source;
}

It is because of the protected modifier, the property source is accesible in the child class Human.


Let us have a look at the below table, which says when and where we can use a particular access modifier.





Access Modifier In a class In a subclass outside of class
Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes