Complete C# Tutorial

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 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 and ShowBrand() 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 and ShowBalance() are private, so they cannot be accessed outside BankAccount.

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 and RevealIdentity() are protected, so they are only accessible inside the Superhero class and its child class Batman.

πŸ”₯ 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 inside Employee).
  • Department is protected (accessible in Employee and Developer).
  • Name and ShowDetails() 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! ➑️πŸ”₯

Leave a Comment

Share this Doc

Access Specifiers in Inheritance

Or copy link