Access Modifier

Access Modifier

In C#, access modifiers are keywords used to control the accessibility of class members (properties, methods, fields, etc.) from other classes or code blocks. There are five access modifiers in C#:

  1. public: Public members are accessible from any code in any assembly. This is the least restrictive access modifier.
  2. private: Private members are accessible only within the class that contains them. This is the most restrictive access modifier.
  3. protected: Protected members are accessible within the class that contains them and any subclasses or derived classes. They are not accessible from outside the class or its derived classes.
  4. internal: Internal members are accessible within the same assembly, but not from other assemblies. This access modifier is useful for creating helper classes or other internal implementation details that should not be exposed outside the assembly.
  5. protected internal: Protected internal members are accessible within the same assembly and any derived classes, even if they are defined in other assemblies.

By using access modifiers, you can control the visibility and accessibility of your class members, which helps you create more secure and maintainable code. Access modifiers also provide a way to hide implementation details and prevent unintended modification of internal state by external code.

 

Example:

				
					using System;
namespace AccessModifiersExample
{
    public class Animal
    {
        private string species; // private field
        protected int age; // protected field
        public string name; // public field
        internal string habitat; // internal field

        public Animal(string species, int age, string name, string habitat)
        {
            this.species = species;
            this.age = age;
            this.name = name;
            this.habitat = habitat;
        }
        public void Eat()
        {
            Console.WriteLine($"{name} is eating.");
        }

        protected void Sleep()
        {
            Console.WriteLine($"{name} is sleeping.");
        }
        private void Move()
        {
            Console.WriteLine($"{name} is moving.");
        }

        internal void DisplayHabitat()
        {
            Console.WriteLine($"{name} lives in {habitat}.");
        }
    }

    public class Dog : Animal
    {
        public Dog(string name, int age, string habitat) : base("Canine", age, name, habitat)
        {

        }

        public void Bark()
        {
            Console.WriteLine($"{name} is barking.");
        }
    }
   class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Animal("Feline", 3, "Lion", "Savannah");
            Dog dog = new Dog("Rufus", 2, "Suburb");
                Console.WriteLine(animal.name); // public field is accessible from outside the class
            animal.Eat(); // public method is accessible from outside the class

      // animal.Move(); // private method is not accessible from outside the class
            // animal.Sleep(); // protected method is not accessible from outside the class

            Console.WriteLine(dog.name); // public field inherited from base class is accessible from derived class
            dog.Bark(); // public method defined in derived class is accessible from derived class
            dog.Eat(); // public method inherited from base class is accessible from derived class

            // dog.Move(); // private method defined in base class is not accessible from derived class
            // dog.Sleep(); // protected method defined in base class is not accessible from derived class

            Console.WriteLine(animal.habitat); // internal field is accessible from the same assembly
            animal.DisplayHabitat(); // internal method is accessible from the same assembly
        }
    }
}

				
			

In this example, we have a base class called “Animal” that has private, protected, public, and internal fields and methods. We also have a derived class called “Dog” that inherits from the “Animal” class.

Here’s a breakdown of the access modifiers used in this example:

  • private: The “species” field and “Move” method in the “Animal” class are marked as private, which means they can only be accessed from within the same class.
  • protected: The “age” field and “Sleep” method in the “Animal” class are marked as protected, which means they can be accessed from within the same class and any derived classes.
  • public: The “name” field and “Eat” method in both the “Animal” and “Dog” classes are marked as public, which means they can be accessed from anywhere in the program.
  • internal: The “habitat” field and “DisplayHabitat” method in the “Animal” class are marked as internal, which means they can be accessed from within the same assembly (project).

By using these access modifiers, we can control the visibility and accessibility of our class members and ensure that our code is secure and maintainable.

Join To Get Our Newsletter
Spread the love