A protected member is visible inside the class and its derived classes.

protected Protected members share state with derived classes without making it public.

Protected Members

interest
ProtectedMembers.cs
Replay: real traced execution (multi-file project)
using System;

class Account
{
    protected int balance;

    public Account(int openingBalance)
    {
        balance = openingBalance;
    }
}

class SavingsAccount : Account
{
    public SavingsAccount(int openingBalance) : base(openingBalance)
    {
    }

    public void AddInterest(int amount)
    {
        balance += amount;
    }

    public int Balance
    {
        get { return balance; }
    }
}

class Program
{
    static void Main()
    {
        int interest = 4;
        SavingsAccount account = new SavingsAccount(20);
        account.AddInterest(interest);

        Console.WriteLine($"interest={interest}");
        Console.WriteLine($"balance={account.Balance}");
    }
}
using System;

class Account
{
    protected int balance;

    public Account(int openingBalance)
    {
        balance = openingBalance;
    }
}

class SavingsAccount : Account
{
    public SavingsAccount(int openingBalance) : base(openingBalance)
    {
    }

    public void AddInterest(int amount)
    {
        balance += amount;
    }

    public int Balance
    {
        get { return balance; }
    }
}

class Program
{
    static void Main()
    {
        int interest = 1;
        SavingsAccount account = new SavingsAccount(20);
        account.AddInterest(interest);

        Console.WriteLine($"interest={interest}");
        Console.WriteLine($"balance={account.Balance}");
    }
}
using System;

class Account
{
    protected int balance;

    public Account(int openingBalance)
    {
        balance = openingBalance;
    }
}

class SavingsAccount : Account
{
    public SavingsAccount(int openingBalance) : base(openingBalance)
    {
    }

    public void AddInterest(int amount)
    {
        balance += amount;
    }

    public int Balance
    {
        get { return balance; }
    }
}

class Program
{
    static void Main()
    {
        int interest = 8;
        SavingsAccount account = new SavingsAccount(20);
        account.AddInterest(interest);

        Console.WriteLine($"interest={interest}");
        Console.WriteLine($"balance={account.Balance}");
    }
}
  1. interest ← 4

    31{32    static void Main()33    {34        int interest→ 4 = 4; //@interest=1, 835        SavingsAccount account = new SavingsAccount(20);36        account.AddInterest(interest);
  2. balance ← 20

    7public Account(int openingBalance20)8{9    balance→ 20 = openingBalance20;10}
  3. public SavingsAccount(int openingBalance) : base(openingBalance)

    14{15    public SavingsAccount(int openingBalance20) : base(openingBalance)16    {17    }
  4. account ← SavingsAccount

    34int interest = 4; //@interest=1, 835SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest4);
  5. balance ← 24

    19public void AddInterest(int amount4)20{21    balance→ 24 += amount4;22}
  6. account.AddInterest(interest);

    35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest4);3738Console.WriteLine($"interest={interest4}");39Console.WriteLine($"balance={account.Balance}");
    outputinterest=4
  7. get

    pass 1 of 5
    25{26    get { return balance24; }27}
  8. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance24}");40}
  9. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance24}");40}
    outputbalance=24
  1. interest ← 1

    31{32    static void Main()33    {34        int interest→ 1 = 1;35        SavingsAccount account = new SavingsAccount(20);36        account.AddInterest(interest);
  2. balance ← 20

    7public Account(int openingBalance20)8{9    balance→ 20 = openingBalance20;10}
  3. public SavingsAccount(int openingBalance) : base(openingBalance)

    14{15    public SavingsAccount(int openingBalance20) : base(openingBalance)16    {17    }
  4. account ← SavingsAccount

    34int interest = 1;35SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest1);
  5. balance ← 21

    19public void AddInterest(int amount1)20{21    balance→ 21 += amount1;22}
  6. account.AddInterest(interest);

    35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest1);3738Console.WriteLine($"interest={interest1}");39Console.WriteLine($"balance={account.Balance}");
    outputinterest=1
  7. get

    pass 1 of 5
    25{26    get { return balance21; }27}
  8. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance21}");40}
  9. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance21}");40}
    outputbalance=21
  1. interest ← 8

    31{32    static void Main()33    {34        int interest→ 8 = 8;35        SavingsAccount account = new SavingsAccount(20);36        account.AddInterest(interest);
  2. balance ← 20

    7public Account(int openingBalance20)8{9    balance→ 20 = openingBalance20;10}
  3. public SavingsAccount(int openingBalance) : base(openingBalance)

    14{15    public SavingsAccount(int openingBalance20) : base(openingBalance)16    {17    }
  4. account ← SavingsAccount

    34int interest = 8;35SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest8);
  5. balance ← 28

    19public void AddInterest(int amount8)20{21    balance→ 28 += amount8;22}
  6. account.AddInterest(interest);

    35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest8);3738Console.WriteLine($"interest={interest8}");39Console.WriteLine($"balance={account.Balance}");
    outputinterest=8
  7. get

    pass 1 of 5
    25{26    get { return balance28; }27}
  8. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance28}");40}
  9. Console.WriteLine($"balance={account.Balance}");

    38    Console.WriteLine($"interest={interest}");39    Console.WriteLine($"balance={account.Balance28}");40}
    outputbalance=28