Showing posts with label access modifiers. Show all posts
Showing posts with label access modifiers. Show all posts

Tuesday, March 30, 2021

Access Modifiers - Encapsulation

We have seen Private, Public in our previous examples or posts. These are access modifiers now let's understand different types of Access Modifiers. 

Access Modifiers helps to achieve encapsulation,

Public             Accessible to all classes
Private           Only accessible to the same class
Protected       It is accessible within the same class and its inherited classes 
Internal           It is accessible within its own assembly, but not from other assembly

We will be going in details to upcoming topic of Inheritance where in broad level we can see the usage of given access modifiers

Class Car
{
        private string color; 
        public string make";
}

Class Program
{
    static void Main(string[] args)
    {
            Car myCar = new Car();
            myCar.color = "Red";      //This will throw error of inaccesible due to its protection level
            myCar.make = "Audi";    //This is allowed
    }
}

Encapsulation can be achieved through modifiers, means you can hide sensitive information from clients. 

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...