Hide the withdraw method in the base class with a method in the subclass of the same name. - CSharp Custom Type

CSharp examples for Custom Type:virtual

Description

Hide the withdraw method in the base class with a method in the subclass of the same name.

Demo Code

using System;//from  ww w .  j  a  va 2  s.  c  o  m
public class Account
{
   protected decimal _balance;
   public Account(decimal initialBalance)
   {
      _balance = initialBalance;
   }
   public decimal Balance
   {
      get { return _balance; }
   }
   public decimal Withdraw(decimal amount)
   {
      decimal amountToWithdraw = amount;
      if (amountToWithdraw > Balance)
      {
         amountToWithdraw = Balance;
      }
      _balance -= amountToWithdraw;
      return amountToWithdraw;
   }
}
public class InvestmentAccount : Account
{
   public decimal _interestRate;
   public InvestmentAccount(decimal initialBalance, decimal interestRate): base(initialBalance)
   {
      _interestRate = interestRate / 100;
   }
   public void AccumulateInterest()
   {
      _balance = Balance + (Balance * _interestRate);
   }
   public decimal Withdraw(decimal withdrawal)
   {
      base.Withdraw(1.5M);
      return base.Withdraw(withdrawal);
   }
}
public class Program
{
   public static void Main(string[] args)
   {
      Account ba;
      InvestmentAccount sa;
      ba = new Account(200M);
      ba.Withdraw(100M);
      sa = new InvestmentAccount(200M, 12);
      sa.Withdraw(100M);
      Console.WriteLine("Account balance is {0:C}", ba.Balance);
      Console.WriteLine("InvestmentAccount balance is {0:C}", sa.Balance);
   }
}

Related Tutorials