Access Specifier in Inheritance in C# β A Beginner-Friendly Guide
π Introduction β Why Do We Need Access Specifiers?
Hey buddy! Have you ever wondered why some things in real life are public, some are private, and some are protected?
Think about your bank account π¦.
- Your account number is visible to you (but not to others).
- Your PIN is private (only you know it).
- The bank manager can access your account details, but not withdraw money.
This is exactly how Access Specifiers in Inheritance in C# work! They control who can access what.
Sounds interesting? Letβs break it down step by step. π
π What You Are Going to Learn in This Lesson:
βοΈ What are Access Specifiers in C#?
βοΈ Why are they important in Inheritance?
βοΈ Different types of access specifiers β Public, Private, Protected
βοΈ Simple and fun examples to understand them easily
Letβs go! β©
π₯ What Are Access Specifiers in C#?
Access specifiers (also called access modifiers) define who can access a class, method, or variable.
In C# Inheritance, these access specifiers determine what gets inherited and how.
There are three main types weβll focus on:
- public β Accessible everywhere
- private β Only accessible inside the class
- protected β Accessible within the class and its derived classes
1οΈβ£ Public Access Specifier β Open to Everyone! π
If something is public, it can be accessed anywhere in the program.
Β
π Example β Car with a Public Method
using System;
class Car
{
public string Brand; // Public variable
public void ShowBrand() // Public method
{
Console.WriteLine($"This car is a {Brand}.");
}
}
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.Brand = "Tesla"; // Accessing public variable
myCar.ShowBrand(); // Accessing public method
}
}
π₯οΈ Output:
This car is a Tesla.
π― Key Takeaway:
Brand
andShowBrand()
are public, so they can be accessed anywhere.
2οΈβ£ Private Access Specifier β Keep It Secret! π€
A private member cannot be accessed outside the class. Itβs only available within the same class.
Β
π Example β Bank Account with Private Balance
using System;
class BankAccount
{
private double balance = 1000; // Private variable
private void ShowBalance() // Private method
{
Console.WriteLine($"Your balance is {balance}.");
}
}
class Program
{
static void Main()
{
BankAccount myAccount = new BankAccount();
// β myAccount.balance = 5000; // ERROR! Can't access private variable
// β myAccount.ShowBalance(); // ERROR! Can't access private method
}
}
π₯οΈ Output:
Compilation error: balance is inaccessible due to its protection level.
π― Key Takeaway:
- The
balance
andShowBalance()
are private, so they cannot be accessed outsideBankAccount
.
3οΈβ£ Protected Access Specifier β Family Only! π¨βπ©βπ¦
A protected member can be accessed inside its own class and in any derived (child) class.
Β
π¦Έ Example β Superhero and Secret Identity
using System;
class Superhero
{
protected string secretIdentity = "Bruce Wayne"; // Protected variable
protected void RevealIdentity() // Protected method
{
Console.WriteLine($"I am {secretIdentity}.");
}
}
// Batman inherits from Superhero
class Batman : Superhero
{
public void Introduce()
{
RevealIdentity(); // Accessing protected method
}
}
class Program
{
static void Main()
{
Batman hero = new Batman();
hero.Introduce(); // Works because Batman inherits Superhero
// β Console.WriteLine(hero.secretIdentity); // ERROR! Can't access directly
}
}
π₯οΈ Output:
I am Bruce Wayne.
π― Key Takeaway:
secretIdentity
andRevealIdentity()
are protected, so they are only accessible inside theSuperhero
class and its child classBatman
.
π₯ Real-World Example β Employee System
Letβs build a simple Employee Management System using all three access specifiers.
using System;
class Employee
{
public string Name; // Public
private double salary; // Private
protected string Department; // Protected
public Employee(string name, double salary, string department)
{
Name = name;
this.salary = salary;
Department = department;
}
public void ShowDetails()
{
Console.WriteLine($"Employee: {Name}, Department: {Department}");
}
private void ShowSalary()
{
Console.WriteLine($"Salary: {salary}");
}
}
// Developer inherits from Employee
class Developer : Employee
{
public Developer(string name, double salary, string department) : base(name, salary, department) {}
public void ShowDepartment()
{
Console.WriteLine($"Developer {Name} works in {Department} department.");
}
}
class Program
{
static void Main()
{
Developer dev = new Developer("Alice", 70000, "IT");
dev.ShowDetails(); // β
Allowed
dev.ShowDepartment(); // β
Allowed
// β dev.ShowSalary(); // ERROR! Private method
// β Console.WriteLine(dev.salary); // ERROR! Private variable
}
}
π₯οΈ Output:
Employee: Alice, Department: IT
Developer Alice works in IT department.
π― Key Takeaway:
salary
is private (only accessible insideEmployee
).Department
is protected (accessible inEmployee
andDeveloper
).Name
andShowDetails()
are public (accessible anywhere).
π Conclusion
β
You learned about Access Specifiers in Inheritance in C#.
β
You now understand public, private, and protected access modifiers.
β
You saw real-world examples like bank accounts, superheroes, and employees!
Feeling more confident? Keep practicing, and youβll master C# inheritance in no time! π
Β
π Next What?
You’re doing amazing! π Now that you understand access specifiers, let’s take it one step further.
In the next chapter, youβll learn about Abstract and Virtual Methods in C# β an exciting way to create flexible and reusable code!
Stay curious! See you in the next lesson! β‘οΈπ₯