Inheritance and Polymorphism
Protected Members
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
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}");
}
}
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);balance ← 20
7public Account(int openingBalance20)8{9 balance→ 20 = openingBalance20;10}public SavingsAccount(int openingBalance) : base(openingBalance)
14{15 public SavingsAccount(int openingBalance20) : base(openingBalance)16 {17 }account ← SavingsAccount
34int interest = 4; //@interest=1, 835SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest4);balance ← 24
19public void AddInterest(int amount4)20{21 balance→ 24 += amount4;22}account.AddInterest(interest);
35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest4);3738Console.WriteLine($"interest={interest4}");39Console.WriteLine($"balance={account.Balance}");outputinterest=4get
pass 1 of 525{26 get { return balance24; }27}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance24}");40}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance24}");40}outputbalance=24
interest ← 1
31{32 static void Main()33 {34 int interest→ 1 = 1;35 SavingsAccount account = new SavingsAccount(20);36 account.AddInterest(interest);balance ← 20
7public Account(int openingBalance20)8{9 balance→ 20 = openingBalance20;10}public SavingsAccount(int openingBalance) : base(openingBalance)
14{15 public SavingsAccount(int openingBalance20) : base(openingBalance)16 {17 }account ← SavingsAccount
34int interest = 1;35SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest1);balance ← 21
19public void AddInterest(int amount1)20{21 balance→ 21 += amount1;22}account.AddInterest(interest);
35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest1);3738Console.WriteLine($"interest={interest1}");39Console.WriteLine($"balance={account.Balance}");outputinterest=1get
pass 1 of 525{26 get { return balance21; }27}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance21}");40}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance21}");40}outputbalance=21
interest ← 8
31{32 static void Main()33 {34 int interest→ 8 = 8;35 SavingsAccount account = new SavingsAccount(20);36 account.AddInterest(interest);balance ← 20
7public Account(int openingBalance20)8{9 balance→ 20 = openingBalance20;10}public SavingsAccount(int openingBalance) : base(openingBalance)
14{15 public SavingsAccount(int openingBalance20) : base(openingBalance)16 {17 }account ← SavingsAccount
34int interest = 8;35SavingsAccount account→ SavingsAccount = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest8);balance ← 28
19public void AddInterest(int amount8)20{21 balance→ 28 += amount8;22}account.AddInterest(interest);
35SavingsAccount account = new SavingsAccount(20);36accountSavingsAccount.AddInterest(interest8);3738Console.WriteLine($"interest={interest8}");39Console.WriteLine($"balance={account.Balance}");outputinterest=8get
pass 1 of 525{26 get { return balance28; }27}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance28}");40}Console.WriteLine($"balance={account.Balance}");
38 Console.WriteLine($"interest={interest}");39 Console.WriteLine($"balance={account.Balance28}");40}outputbalance=28