Prevent overriding a method of a class. This is a method that is declared with
the keyword sealed and is always used with combination of override keyword.
Derived classes will not be able to override this method as it is sealed for
overriding.
Example by me:
class Account
{
public int Number { get; set; }
public string Holder { get; set; }
public double Balance { get; protected set; } // protected property is used to avoid the access to the balance from outside the class
public Account()
{
}
public Account (int Number, string Holder, double Balance)
{
this.Number = Number;
this.Holder = Holder;
this.Balance = Balance;
}
public virtual void Withdraw(double amount)
{
if (amount > Balance)
{
Console.WriteLine("The amount exceeds the balance");
}
else
{
Balance -= amount;
}
}
}
namespace TestConsoleApp.Entities.AccountProject;
sealed class SavingsAcount : Account
{
public double InterestRate { get; set; }
public SavingsAcount()
{
}
public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
{
this.InterestRate = InterestRate;
}
public void UpdateBalance()
{
Balance += Balance * InterestRate;
}
public override void Withdraw(double amount)
{
base.Withdraw(amount);
//UpdateBalance();
}
}
sealed class SavingsAcount : Account
{
public double InterestRate { get; set; }
public SavingsAcount()
{
}
public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
{
this.InterestRate = InterestRate;
}
public void UpdateBalance()
{
Balance += Balance * InterestRate;
}
public sealed override void Withdraw(double amount)
{
base.Withdraw(amount);
//UpdateBalance();
}
}
With this 2 examples, you could make a third example trying to use the
method Widtdraw of SavingsAccount if you would pretend to derivate
SavingAccount class but you couldn't do that.
Prevent overriding a method of a class. This is a method that is declared with
the keyword sealed and is always used with combination of override keyword.
Derived classes will not be able to override this method as it is sealed for
overriding.
Example by me:
class Account
{
public int Number { get; set; }
public string Holder { get; set; }
public double Balance { get; protected set; } // protected property is used to avoid the access to the balance from outside the class
public Account()
{
}
public Account (int Number, string Holder, double Balance)
{
this.Number = Number;
this.Holder = Holder;
this.Balance = Balance;
}
public virtual void Withdraw(double amount)
{
if (amount > Balance)
{
Console.WriteLine("The amount exceeds the balance");
}
else
{
Balance -= amount;
}
}
}
namespace TestConsoleApp.Entities.AccountProject;
sealed class SavingsAcount : Account
{
public double InterestRate { get; set; }
public SavingsAcount()
{
}
public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
{
this.InterestRate = InterestRate;
}
public void UpdateBalance()
{
Balance += Balance * InterestRate;
}
public override void Withdraw(double amount)
{
base.Withdraw(amount);
//UpdateBalance();
}
}
sealed class SavingsAcount : Account
{
public double InterestRate { get; set; }
public SavingsAcount()
{
}
public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
{
this.InterestRate = InterestRate;
}
public void UpdateBalance()
{
Balance += Balance * InterestRate;
}
public sealed override void Withdraw(double amount)
{
base.Withdraw(amount);
//UpdateBalance();
}
}
With this 2 examples, you could make a third example trying to use the
method Widtdraw of SavingsAccount if you would pretend to derivate
SavingAccount class but you couldn't do that.