How to use inheritance in c#

Implementing Inheritance in C#.net is similar to that in Visual Basic.net. However, there is a difference in syntax while creating base class and derived class.

Click here to know How to use inheritance in Visual Basic.Net.

Note: We cannot use the ':' symbol to derive another class in VisulBasic.Net. Instead, we have to use the 'Inherits' statement to derive the class.

The following code creates the base class in C# - Account Class
class Account
    {
        private string mCode;
        private string mName;
        private string mdescription;
        private double mBalance;

        public Account(string code, string name, string description, double balance)
        {
            mCode = code;
            mName = name;
            mdescription = description;
            mBalance = balance;
        }
       


Creation of the derived class in C# - BankAccount

class BankAccount : Account
    {
        public void deposit(double amount)
        {
            double newBalance;
            newBalance = Balance + amount;
            Balance = newBalance;
        }
       
        public void withdraw(double amount)
        {
            double newBalance;
            newBalance = Balance - amount;
            Balance = newBalance;
        }

    }
	
	
To see how inheritance works in C#, create a windows form and name it as BankForm. Design the form as shown in the Fig below. The code for the BankForm is given below.

windows form named as Bank Form to demonstrate inheritance in C#

public partial class BankForm : Form
    {
         
        BankAccount myBankAcc = new BankAccount();
        public BankForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            myBankAcc.Code = txtCode.Text;
            myBankAcc.Name = txtName.Text;
            myBankAcc.description = txtdescription.Text;
            myBankAcc.Balance = Convert.ToDouble(txtBalance.Text);
        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {
            myBankAcc.deposit(100);
            MessageBox.Show(myBankAcc.Getbalance().ToString());

        }

        private void btnWithdraw_Click(object sender, EventArgs e)
        {
          myBankAcc.deposit(50);
          MessageBox.Show(myBankAcc.Getbalance().ToString());
        }
    }
}