Access Modifiers

Access modifiers in C# are keywords that define the accessibility of types and their members, such as classes, methods, properties, and fields. They are essential for encapsulating and protecting data, ensuring that access to the internal workings of a class is controlled and appropriately limited. The five main access modifiers in C# are public, private, protected, internal, and protected internal.

Access Modifiers Explained

Public (public):
  • The public access modifier allows a type or member to be accessed from anywhere in the code, both within the same assembly and from other assemblies. It provides the least restrictive access level.
  • Example: Public properties and methods can be accessed by any class in the project, or by classes in other projects.
Private (private):
  • The private access modifier restricts access to the containing class. This means that a private member is accessible only within the class where it is declared. It provides the most restrictive access level.
  • Example: Private fields are used to hide the internal state of a class and can be accessed or modified only through public or protected methods.
Protected (protected):
  • The protected access modifier allows access within its own class and by instances of derived classes. This modifier is useful in scenarios where you want to allow subclasses to access certain members of the base class while keeping them hidden from other classes.
  • Example: Protected methods can be accessed and overridden by derived classes to provide specialized behavior.
Internal (internal):
  • The internal access modifier allows access only within the same assembly, preventing access from other assemblies. It is useful for defining members that are intended for use exclusively within the context of the project where they are defined.
  • Example: Internal classes and methods are accessible within the same project but hidden from other projects.
Protected Internal (protected internal):
  • The protected internal access modifier permits access within the same assembly and by instances of derived classes, even if those classes are in different assemblies. This modifier combines the behaviors of both protected and internal.
  • Example: Protected internal members can be accessed by any class in the same project and by derived classes in other projects.

Example

using System;

namespace AccessModifiersExample
{
  public class Person
  {
      // Public member accessible from anywhere
      public string Name { get; set; }

        // Private member accessible only within the Person class
      private int age;

        // Protected member accessible within Person and derived classes
      protected string Address { get; set; }

        // Internal member accessible within the same assembly
      internal string Nationality { get; set; }

        // Protected internal member accessible within the same assembly and by derived classes
      protected internal string Email { get; set; }

        // Public constructor
      public Person(string name, int age, string address, string nationality, string email)
      {
          Name = name;
          this.age = age;
          Address = address;
          Nationality = nationality;
          Email = email;
      }

        // Public method
      public void DisplayInfo()
      {
          Console.WriteLine($"Name: {Name}, Age: {age}, Address: {Address}, Nationality: {Nationality}, Email: {Email}");
      }

        // Protected method
      protected void DisplayProtectedInfo()
      {
          Console.WriteLine($"Address: {Address}");
      }

        // Internal method
      internal void DisplayInternalInfo()
      {
          Console.WriteLine($"Nationality: {Nationality}");
      }

        // Protected internal method
      protected internal void DisplayProtectedInternalInfo()
      {
          Console.WriteLine($"Email: {Email}");
      }
  }

    // Derived class
  public class Employee : Person
  {
      // Public member specific to Employee
      public int EmployeeID { get; set; }

        // Public constructor
      public Employee(string name, int age, string address, string nationality, string email, int employeeID)
          : base(name, age, address, nationality, email)
      {
          EmployeeID = employeeID;
      }

        // Public method
      public void DisplayEmployeeInfo()
      {
          // Accessing protected and protected internal members from the base class
          Console.WriteLine($"Employee ID: {EmployeeID}, Name: {Name}, Address: {Address}, Email: {Email}");
      }
  }

    class Program
  {
      static void Main()
      {
          // Creating an object of the Employee class
          Employee emp = new Employee("John Doe", 30, "123 Main St", "American", "john.doe@example.com", 12345);

            // Accessing public members
          Console.WriteLine(emp.Name);
          Console.WriteLine(emp.EmployeeID);

            // Accessing internal member
          Console.WriteLine(emp.Nationality);

            // Calling public method
          emp.DisplayInfo();
          emp.DisplayEmployeeInfo();

            // Calling protected internal method
          emp.DisplayProtectedInternalInfo();

            // The following line would cause a compilation error because age is private
          // Console.WriteLine(emp.age);

            // The following line would cause a compilation error because Address is protected and cannot be accessed directly from an instance of Employee
          // Console.WriteLine(emp.Address);
      }
  }
}

Explanation

Person Class:
  • Name: A public property that can be accessed from anywhere.
  • Age: A private field that is only accessible within the Person class.
  • Address: A protected property that can be accessed within the Person class and any derived classes.
  • Nationality: An internal property that is accessible only within the same assembly.
  • Email: A protected internal property that can be accessed within the same assembly and by derived classes.
  • DisplayInfo Method: Prints all the information about the Person.
  • DisplayProtectedInfo Method: Demonstrates access to protected members.
  • DisplayInternalInfo Method: Demonstrates access to internal members.
  • DisplayProtectedInternalInfo Method: Demonstrates access to protected internal members.
Employee Class:
  • Inherits from the Person class.
  • EmployeeID: A public property that can be accessed from anywhere.
  • DisplayEmployeeInfo Method: Accesses protected and protected internal members inherited from the Person class.
Main Method:
  • Creates an instance of the Employee class and demonstrates how to access public and internal members while calling public and protected internal methods. Attempting to access private and protected members directly would result in compilation errors.
Post a comment

Leave a Comment

Scroll to Top