no image

Questions :
Why we need access modifiers in programming ?
Real world example of access modifiers ?
Why should we use access modifiers?
How to use access modifiers ?
When should we use access modifiers ?
Is access modifiers related to real world example ?
How to use access modifiers ?
Which type of problem we can solved by access modifiers ?

Use
Bind the limit for access/ Define the limit 

Example in real world
Imagine You are the employee of software company.
Below are some point , you can ask one Question to you (Who can have access ….?)
  1. Canteen/ reception / facility (gym/ dormitory / entertainment zone )
  2. Your personal cubical / chair / laptop
  3. Team Project board/ team labs room
  4. Other Team members
  5. Can you work with other team members
  6. Project Discussion with your colleague in your private cubical


Area In Company
For you
For Other Employee
Canteen/ reception / facility (gym/ dormitory / entertainment zone )
YES
YES, It is Public
Your personal cubical / chair / laptop
YES
NO, Other employee can't access your Private cubical/ chair/ laptop
Team Project board / team labs / meeting room
YES
NO, Only you and your team members (Protected) are allowed
Share team labs or other stuff within team
YES
NO (Internal)
Can you work with other team members
NO/ YES- *if project demands
NO/ YES- if project demands (Protected Internal)
Project Discussion with your colleague in your private cubical
NO / YES -*In case your permission/call
NO/ YES -In case your permission/call (Private Protected)


In Programming, How to use Access modifiers in Programming ?
Public 
Key Point to remember
1. If you define class as public All variable/ function not public
2. Explicitly define public to class Function / variables 
3. By default, Interface is public

//Public Access Modifiers
public class facility
{
        public string Canteen = "Canteen is Public";
        public string Gym = "Gym is Public";
        public string Entertainment_Zone = "Entertainment is Public";
}
   
class Program:Team
{
        static void Main(string[] args)
        {
            facility facilityforMe = new facility();
            Console.WriteLine(facilityforMe.Canteen);
            Console.WriteLine(facilityforMe.Gym);
            Console.WriteLine(facilityforMe.Entertainment_Zone);
        }

}

Private
Key Point to remember
1. Can't define Private class top-level (Just below the namespace)
2. If access modifiers not defined, by default it is private

//Private Access
class PrivateStuff
{
        private string Chair = "Chair is Private ";
        private string Laptop = "Laptop is Private ";
        //By default private variable
        string Cubical = "Cubical is Private ";
        //use of private variable within class
        public string GetMyPrivateStuff()
        {
            return (Chair + Laptop + Cubical);
        }
}
class Program
{
        static void Main(string[] args)
        {
            PrivateStuff MyprivateStuff = new PrivateStuff();
            //We can't access private variables outside the PrivateStuff Class
            //MyprivateStuff.Chair / Laptop / Cubical
            Console.WriteLine(MyprivateStuff.GetMyPrivateStuff());
        }
}

Protected
Key Point to remember 
   1. It acts as private within class 
   2. Inherit from base class (ProtectedClassTeam) 
   3. It act as public in derived class(GetProtectedTeamDetails) only

class ProtectedClassTeam
{
        protected string ProtectedTeamMember = "Team member is protected ";
        protected string ProtectedMeetingRoom = "Meeting Room is Protected ";
}
class GetProtectedTeamDetails:ProtectedClassTeam
{
        public string ShowTeamDetails()
        {
            return ProtectedMeetingRoom +ProtectedTeamMember;
        } 
}
class Program:Team
{
        static void Main(string[] args)
        {
            GetProtectedTeamDetails getProtectedTeamDetails =
                                                                        new GetProtectedTeamDetails();
            getProtectedTeamDetails.ShowTeamDetails();          
            ProtectedClassTeam protectedClassTeam = new ProtectedClassTeam();
            //we can't access protected variable here
            //protectedClassTeam.ProtectedTeamMember / ProtectedMeetingRoom
         }
}

Internal 

Key Point to remember
In case you change the namespace. It will not work as expected
If namespace is different then, you need to give reference in used class


*Here is two assembly Company_AccessModifiers and TeamA
Now we are creating internal class and variable which is accessible for
Company_AccessModifiers Only 
If we add new TeamInternalStuff class in Company_AccessModifiers Declare internal variable as below

namespace Company_AccessModifiers 
{
    internal class TeamInternalStuff
    {
        internal string TeamInternalVariable = 
                           "Labs internally used within team only";
    }
}
//Accessible in Program.cs
class Program:Team
{
        static void Main(string[] args)
        {
TeamInternalStuff myInternalClass = new TeamInternalStuff();
        Console.WriteLine(myInternalClass.InternalVariable);
         }
}

//Not Accessible in TeamA Assembly
namespace TeamA
{
    public class TeamAMembers
    {       
        public void GetDetails()
        {  
            //TeamInternalStuff not available 
        }
    }
}

Protected Internal
Key Point to remember
It work on across namespaces


*Here is two assembly Company_AccessModifiers and TeamA
Now we are creating Protected internal variable
which is accessible for TeamA
*If we add new ProtectedInternalTeam class in TeamA
Declare internal variable as below


namespace TeamA
{
    public class ProtectedInternalTeam
    {
        protected internal string ProtectedInternalTeamAMember =
 " Protected Internl TeamA Member ";
    }
    //Internally access within the assembly
    public class TeamAInternalStuff
    {
        public void GetTeamStuff()
        {
            ProtectedInternalTeam protectedInternalTeam = 
                     new  ProtectedInternalTeam();
            Console.WriteLine(
                     protectedInternalTeam.ProtectedInternalTeamAMember);
        }
    }
}

//Accessible in Company_AccessModifiers  Assembly
class Program:ProtectedInternalTeam
 {
        static void Main(string[] args)
        {
              Console.WriteLine(ProtectedInternalTeamAMember);       
        }
 }

Private Protected 

Key Point to remember
1.      We can use it c#7.2 version
2.      It is same feature as private and protected

class PrivateStuff
{
    private protected string CubicalForMeet =
    "Project Discussion in your private cubical 
     with your coulegue ";
}
class Program:PrivateStuff
{
        static void Main(string[] args)
        {
              Console.WriteLine(CubicalForMeet);
        }
}
sblog4u. Powered by Blogger.

Topics

Real World Programming Concept in 2 mins

Clone

What is Clone ? It look like duplicate of original Reference is same for string And different for objects Once clone crea...