KEMBAR78
Access Modifiers | PDF | Class (Computer Programming) | Object Oriented Programming
0% found this document useful (0 votes)
10 views6 pages

Access Modifiers

This document serves as a beginner's guide to Object-Oriented Programming (OOP) in C#, covering key concepts such as classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It explains access modifiers in C#, detailing their visibility and accessibility with examples for each modifier: public, private, protected, internal, protected internal, and private protected. Additionally, it provides real-world analogies to illustrate the differences between these access modifiers.

Uploaded by

HABIBA SIDDIQI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Access Modifiers

This document serves as a beginner's guide to Object-Oriented Programming (OOP) in C#, covering key concepts such as classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It explains access modifiers in C#, detailing their visibility and accessibility with examples for each modifier: public, private, protected, internal, protected internal, and private protected. Additionally, it provides real-world analogies to illustrate the differences between these access modifiers.

Uploaded by

HABIBA SIDDIQI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Object-Oriented Programming (OOP) in C# - Beginner Guide

1. Introduction to OOP

Object-Oriented Programming (OOP) is a programming paradigm centered around objects and


classes. It helps organize code for better reusability, scalability, and maintainability.

Key Concepts:

 Class: A blueprint for creating objects

 Object: An instance of a class

 Encapsulation

 Abstraction

 Inheritance

 Polymorphism

2. Access Modifiers in C#

Access modifiers control the visibility and accessibility of classes, methods, and other members.

Modifier Description

public Accessible from anywhere in the project or other assemblies.

private Accessible only within the containing class.

protected Accessible within the class and in derived classes.

internal Accessible within the same assembly/project.

protected internal Accessible in the same assembly OR in a derived class elsewhere.

private protected Accessible within the same class or derived classes in the same assembly.

Examples for Each Modifier

Public Example

public class PublicExample

public string message = "Accessible everywhere";

class Test

static void Main()


{

PublicExample obj = new PublicExample();

Console.WriteLine(obj.message); // Accessible

Private Example

public class PrivateExample

private string secret = "This is private";

public void ShowSecret()

Console.WriteLine(secret); // Accessible here

class Test

static void Main()

PrivateExample obj = new PrivateExample();

// Console.WriteLine(obj.secret); // ❌ Not accessible

obj.ShowSecret(); // ✅ Accessible via method

Protected Example

public class BaseClass

protected string data = "Protected data";

}
public class DerivedClass : BaseClass

public void ShowData()

Console.WriteLine(data); // Accessible in derived class

Internal Example

internal class InternalExample

internal string info = "Internal info";

class Test

static void Main()

InternalExample obj = new InternalExample();

Console.WriteLine(obj.info); // Accessible within same assembly

Protected Internal Example

public class ProtectedInternalBase

protected internal string details = "Protected Internal Data";

public class Accessor : ProtectedInternalBase

public void PrintDetails()

{
Console.WriteLine(details); // Accessible in derived class

Private Protected Example

public class PrivateProtectedBase

private protected string data = "Private Protected Data";

protected class Nested : PrivateProtectedBase

public void ShowData()

Console.WriteLine(data); // Accessible within derived class in same assembly

These examples illustrate how each access modifier behaves in different contexts, especially in terms
of inheritance and assembly boundaries. Access modifiers control the visibility and accessibility of
classes, methods, and other members.

Modifier Description

public Accessible from anywhere in the project or other assemblies.

private Accessible only within the containing class.

protected Accessible within the class and in derived classes.

internal Accessible within the same assembly/project.

protected internal Accessible in the same assembly OR in a derived class elsewhere.

private protected Accessible within the same class or derived classes in the same assembly.

Example

public class Employee

private string id = "E101";

protected string department = "HR";


internal string office = "Mumbai";

protected internal string email = "abc@x.com";

private protected int level = 2;

public string name = "John Doe";

public void ShowDetails()

Console.WriteLine($"ID: {id}");

Console.WriteLine($"Dept: {department}");

Console.WriteLine($"Office: {office}");

Console.WriteLine($"Email: {email}");

Console.WriteLine($"Level: {level}");

Console.WriteLine($"Name: {name}");

Accessing in Another Class (Same Project)

class Program

static void Main()

Employee emp = new Employee();

// Console.WriteLine(emp.id); // ❌ private – not accessible

// Console.WriteLine(emp.department); // ❌ protected – not accessible

Console.WriteLine(emp.office); // ✅ internal

Console.WriteLine(emp.email); // ✅ protected internal

// Console.WriteLine(emp.level); // ❌ private protected – not accessible

Console.WriteLine(emp.name); // ✅ public

Real-World Analogy
 private: Your phone’s passcode – only you can access.

 protected: Family secrets – shared only with your children.

 internal: A company’s internal portal – only employees can access.

 protected internal: Like a business alliance – partners and employees.

 private protected: A family business – only relatives working in the company.

 public: Public billboard – anyone can see.

You might also like