Introduction
Three parts in, and our little coffee shop has grown quite a personality. We've overloaded the Barista, inherited from MenuItem, watched SpecialMenuItem override and hide its way through virtual chains, and even taught Beverage that you can't order "a beverage" without picking something specific.
This time, we're locking the doors — literally. Every class, every method, every field in a real coffee shop has someone who's allowed to touch it and a dozen people who aren't. The cashier shouldn't be able to rewrite the secret espresso recipe. A customer definitely shouldn't be able to call the "apply staff discount" method. That's exactly the job of access modifiers — public, private, protected, internal, protected internal, sealed, plus the closely related const, static, and readonly fields.
Fair warning again — this is a long one, possibly the longest yet, because access modifiers touch everything: classes, members, inheritance, even fields. But we'll do what we always do — one small, run-it-yourself code snippet at a time, and by the end you'll know this topic by heart.
Grab your coffee. Let's lock some things down.
Here's the series roadmap:
Diving in OOP (Access Modifiers) — you're here
Diving in OOP (Properties)
Note: Every code snippet here has actually been run — no theoretical guessing, just real compiler output (including the errors, especially the errors).
Series Articles
What Are Access Modifiers?
Straight from the textbook definition: access modifiers are keywords that set the accessibility of classes, methods, and other members. They're the whole reason encapsulation works — without them, every part of your coffee shop's code could reach into every other part, and nothing would ever stay private.
Let's take each one into the shop, one at a time.
Public, Private, and Protected at the Class Member Level
Whenever we write a class, we want control over who can touch its members. The one thumb rule to remember: members of the same class can always access each other freely, no restrictions, no exceptions.
Create a console application named CoffeeShopApp. Add a class called OrderProcessor,
namespace CoffeeShopApp
{
class OrderProcessor
{
static void CalculateTax()
{
Console.WriteLine("OrderProcessor CalculateTax");
}
public static void ProcessOrder()
{
Console.WriteLine("OrderProcessor ProcessOrder");
CalculateTax();
}
}
class Program
{
static void Main(string[] args)
{
OrderProcessor.ProcessOrder();
}
}
}
Output:
OrderProcessor ProcessOrder
OrderProcessor CalculateTax
ProcessOrder is marked public, so anyone can call it. CalculateTax has no modifier at all — which quietly makes it private by default. That private-ness has zero effect within the same class, so ProcessOrder calling CalculateTax works just fine. This is called member access.
Point to Remember: The default access modifier for class members is
private.
Now try calling CalculateTax directly from Program,
class Program
{
static void Main(string[] args)
{
OrderProcessor.CalculateTax();
}
}
Output:
Compile time error: 'CoffeeShopApp.OrderProcessor.CalculateTax()' is inaccessible due to its protection level
Locked out, exactly as expected. Now mark CalculateTax explicitly protected instead of leaving it default,
protected static void CalculateTax()
{
Console.WriteLine("OrderProcessor CalculateTax");
}
Output:
Compile time error: 'CoffeeShopApp.OrderProcessor.CalculateTax()' is inaccessible due to its protection level
Same error — protected still won't let Program in, because Program isn't a derived class. But ProcessOrder, sitting inside the same class, can still call it without issue.
Modifiers in Inheritance
Let's bring a derived class into the picture — a SpecialMenuItem-style relationship, but themed for order processing this time,
class OrderProcessorBase
{
static void CalculateTax()
{
Console.WriteLine("OrderProcessorBase CalculateTax");
}
public static void ApplyBasePrice()
{
Console.WriteLine("OrderProcessorBase ApplyBasePrice");
}
protected static void ApplyStaffDiscount()
{
Console.WriteLine("OrderProcessorBase ApplyStaffDiscount");
}
}
class OrderProcessorDerived : OrderProcessorBase
{
public static void ShowFullDetails()
{
CalculateTax();
ApplyBasePrice();
ApplyStaffDiscount();
}
}
class Program
{
static void Main(string[] args)
{
OrderProcessorDerived.ShowFullDetails();
}
}
Output:
Compile time error: 'CoffeeShopApp.OrderProcessorBase.CalculateTax()' is inaccessible due to its protection level
ApplyStaffDiscount (our protected method) compiles fine from inside ShowFullDetails — that's exactly what protected is for, granting access to derived classes. But CalculateTax, private by default, remains locked even from the derived class. Remove that one call and the code compiles cleanly.
Point to Remember:
privategrants access only to the same class.publicgrants access to everyone.protectedsits in between — only derived classes get in.
The Internal Modifier at Class Level
Now let's go cross-assembly. Create a class library project named CoffeeShopLibrary, and add a class called InventoryItem, marked internal,
CoffeeShopLibrary.InventoryItem:
namespace CoffeeShopLibrary
{
internal class InventoryItem
{
}
}
Compile the library, then reference its .dll from your CoffeeShopApp console project.
CoffeeShopApp.Program:
using CoffeeShopLibrary;
namespace CoffeeShopApp
{
class Program
{
static void Main(string[] args)
{
InventoryItem item;
}
}
}
Output:
Compile time error: 'CoffeeShopLibrary.InventoryItem' is inaccessible due to its protection level
internal restricts access to the assembly it was declared in, and nothing outside it — not even a project that references the DLL. Remove the internal keyword entirely and try again,
namespace CoffeeShopLibrary
{
class InventoryItem
{
}
}
Output:
Compile time error: 'CoffeeShopLibrary.InventoryItem' is inaccessible due to its protection level
Same error — because a class with no modifier at all is internal by default. Had we marked InventoryItem as public, everything would compile without complaint.
Point to Remember: A class marked
internallimits access to the current assembly only.
Namespaces With Modifiers
Just for fun, let's try slapping public on a namespace,
public namespace CoffeeShopApp
{
class Program
{
static void Main(string[] args)
{
}
}
}
Output:
Compile time error: A namespace declaration cannot have modifiers or attributes
Point to Remember: Namespaces have no accessibility specifiers at all. They're implicitly public everywhere, and you can't add any modifier — not even
publicitself.
Private Class
One more experiment — mark Program itself as private,
namespace CoffeeShopApp
{
private class Program
{
static void Main(string[] args)
{
}
}
}
Output:
Compile time error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
Point to Remember: A top-level class can only be
publicorinternal— neverprotectedorprivate. The default isinternal.
Access Modifiers for Class Members
Members, on the other hand, are far more flexible — they can carry any of the access modifiers, defaulting to private.
Point to Remember: Members of a class can be marked with any access modifier; the default is
private.
What happens if we try to stack two modifiers on a method?
public class Program
{
static void Main(string[] args)
{
}
public private void ProcessOrder()
{
}
}
Output:
Compile time error: More than one protection modifier
Not allowed — with one notable exception coming up shortly (protected internal). Note also that built-in types like int and object carry no accessibility restrictions of their own — they're usable anywhere.
Internal Class, Public Method
Back to CoffeeShopLibrary. Mark InventoryItem internal, but give it a public method,
namespace CoffeeShopLibrary
{
internal class InventoryItem
{
public void GetStock() { }
}
}
using CoffeeShopLibrary;
namespace CoffeeShopApp
{
public class Program
{
public static void Main(string[] args)
{
InventoryItem item = new InventoryItem();
item.GetStock();
}
}
}
Output
Compile time errors:
'CoffeeShopLibrary.InventoryItem' is inaccessible due to its protection level
The type 'CoffeeShopLibrary.InventoryItem' has no constructors defined
'CoffeeShopLibrary.InventoryItem' is inaccessible due to its protection level
'CoffeeShopLibrary.InventoryItem' does not contain a definition for 'GetStock' ...
A pile of errors, but the takeaway is simple: even a public method is useless from outside the assembly if its containing class is internal. The class's protection level always wins.
Public Class, Private Method
Flip it — public class, private method,
namespace CoffeeShopLibrary
{
public class InventoryItem
{
private void GetStock() { }
}
}
Output
Compile time error: 'CoffeeShopLibrary.InventoryItem' does not contain a definition for 'GetStock' ...
Making the class public doesn't rescue a private method. Both the class and the member's accessibility need to line up for outside access to succeed.
Public Class, Internal Method
namespace CoffeeShopLibrary
{
public class InventoryItem
{
internal void GetStock() { }
}
}
Output:
Compile time error: 'CoffeeShopLibrary.InventoryItem' does not contain a definition for 'GetStock' ...
Same story — an internal member is invisible outside the DLL it was compiled into, public class or not.
Protected Internal
Now the combo modifier. In CoffeeShopLibrary, set up three classes,
namespace CoffeeShopLibrary
{
public class MenuItem
{
protected internal void PrepareRecipe()
{
}
}
public class SpecialMenuItem : MenuItem
{
protected internal void PrepareSpecialRecipe()
{
PrepareRecipe();
}
}
public class BillingService
{
public void GenerateBill()
{
MenuItem item = new MenuItem();
item.PrepareRecipe();
}
}
}
using CoffeeShopLibrary;
namespace CoffeeShopApp
{
public class Program
{
public static void Main(string[] args)
{
BillingService billing = new BillingService();
billing.GenerateBill();
}
}
}
Output: Compiles with no error.
protected internal grants access to two groups at once — a derived class (SpecialMenuItem), and any class within the same assembly (BillingService, sitting right there in CoffeeShopLibrary).
Point to Remember:
protected internalmeans the derived class and any class within the same assembly can both access the member.
Protected Member
namespace CoffeeShopApp
{
class Recipe
{
protected int secretIngredientCount;
void MethodRecipe(Recipe recipe, SignatureRecipe signatureRecipe)
{
recipe.secretIngredientCount = 100;
signatureRecipe.secretIngredientCount = 200;
}
}
class SignatureRecipe : Recipe
{
void MethodSignature(Recipe recipe, SignatureRecipe signatureRecipe)
{
recipe.secretIngredientCount = 100;
signatureRecipe.secretIngredientCount = 200;
}
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Output
Compile time error: Cannot access protected member 'CoffeeShopApp.Recipe.secretIngredientCount'
via a qualifier of type 'CoffeeShopApp.Recipe'; the qualifier must be of type
'CoffeeShopApp.SignatureRecipe' (or derived from it)
Inside Recipe, no modifier restricts anything (same-class access is always free). But inside the derived SignatureRecipe, accessing the field through a plain Recipe-typed reference (recipe.secretIngredientCount) fails — while accessing it through a SignatureRecipe-typed reference (signatureRecipe.secretIngredientCount) works fine. Comment out the failing line and the rest compiles.
Point to Remember: You cannot access a
protectedmember through a base-class reference — only through the derived class's own reference, even from within the derived class itself.
Accessibility Priority in Inheritance
namespace CoffeeShopApp
{
class InventoryItem
{
}
public class SpecialInventoryItem : InventoryItem
{
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Output:
Compile time error: Inconsistent accessibility: base class 'CoffeeShopApp.InventoryItem'
is less accessible than class 'CoffeeShopApp.SpecialInventoryItem'
InventoryItem is internal by default; SpecialInventoryItem is explicitly public. A derived class can never be more accessible than its own base class.
Point to Remember: The base class must always allow at least as much accessibility as any class deriving from it.
Swap the modifiers — base public, derived internal — and the error disappears entirely.
Another scenario — return types,
namespace CoffeeShopApp
{
class InventoryItem
{
}
public class BillingService
{
public InventoryItem GetItem()
{
InventoryItem item = new InventoryItem();
return item;
}
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Output
Compile time error: Inconsistent accessibility: return type 'CoffeeShopApp.InventoryItem'
is less accessible than method 'CoffeeShopApp.BillingService.GetItem()'
Point to Remember: A method's return type must have accessibility at least as generous as the method itself.
And once more with a field,

Sudarshan HajarePosted Aug 26, 2026, 4:46 AM
Thanks Jasen