How to use inheritance in Visual C#


This step-by-step article shows you how to use inheritance in Visual C#.

Inheritance is an important object-oriented concept. It allows you to build a hierarchy of related classes, and to reuse functionality defined in existing classes.

In the following examples, you will define a base class that contains fields and methods for a generic bank account. You will then define a derived class that represents a particular kind of bank account. The derived class will inherit members from the base class, override selected members, and add new members for the new kind of account.


Prior knowledge required:
  • C# syntax
  • Object-oriented concepts

Create a New Console Application

  1. Start Visual Studio and create a new C# console application project named UseInheritance.
  2. Save the project.

Create an Abstract Base Class

  1. In Solution Explorer, switch to the Class View (If you cannot see it, click Class View in the View menu). Right-click the UseInheritance project, point to Add, and then click Class.

    Note In Visual C# .NET 2003, point to Add, and then click Add Class.
  2. In the Add New Item dialog, type in a class name of Account, and then click Add.

    The Account class is displayed. Notice the keyword. Add the public and abstract modifiers to the class so it resembles the following.
    public abstract class Account 


Write Code for the Base Class

  1. In the Code View window, add two fields to the Account class to represent the account balance and the name of the account holder:
    private string name;        // Only accessible in base class
    protected double balance; // Accessible in base class and derived class
  2. Modify the constructor as follows to initialize these fields:
    public Account(string nm, double bal)
    { = nm;
    this.balance = bal;

  3. Add the following methods to the class. The virtual keyword means these methods can be overridden in derived classes:
    public virtual void Credit(double amount)
    this.balance += amount;

    public virtual void Debit(double amount)
    this.balance -= amount;

    public virtual void Display()
    Console.WriteLine("Name={0}, balance={1}",, this.balance);

  4. Add the following method to the class. Because this method is not marked as virtual, it cannot be overridden in derived classes. This method provides the capability to change the name of the account holder.
    public void ChangeName(string newName)
    { = newName;

  5. Add the following method to the class. The abstract keyword means this method must be overridden in derived classes:
    public abstract double CalculateBankCharge();

Create a Derived Class

  1. In the Class View, right-click the UseInheritance project. On the Shortcut menu, choose Add and then click Class.
  2. In the Add New Item dialog box, type SavingsAccount, and then click Add.
  3. View the code for the SavingsAccount class.
  4. Change the SavingsAccount class definition as follows, so that SavingsAccount inherits from Account:
    public class SavingsAccount : Account


Write Code for the Derived Class

  1. Add a field to the SavingsAccount class:
    private double minBalance;   // If the balance drops below minBalance, 
    // the bank will charge a fee on the account
  2. Modify the SavingsAccount constructor to initialize the fields in the base class and in this class:
    public SavingsAccount(string nm, double bal, double min) 
    : base(nm, bal) // Call base-class constructor first
    minBalance = min; // Then initialize fields in this class

  3. Add the following methods to the SavingsAccount class. These methods override the virtual methods inherited from the base class:
    public override void Debit(double amount)
    if (amount <= balance) // Use balance, inherited from base class
    base.Debit(amount); // Call Debit, inherited from base class
    public override void Display()
    base.Display(); // Call Display, inherited from base class
    Console.WriteLine("$5 charge if balance goes below ${0}", minBalance);

  4. You must override all abstract methods from the base class. Add the following method to the SavingsAccount class:
    public override double CalculateBankCharge()
    if (balance < minBalance)
    return 5.00;
    return 0.00;


Verify It Works

  1. Display the code for Class1.cs in the Code View window.
  2. In the Main method, create a SavingsAccount object and display it as follows:
    SavingsAccount sa = new SavingsAccount("Freda Smith", 100.00, 25);
  3. Add the following code to call public methods in SavingsAccount or Account:
    sa.ChangeName("Freda Jones");
    Console.WriteLine("Bank charge: ${0}", sa.CalculateBankCharge());
  4. Build the application.
  5. Set a breakpoint at the start of the Main method, and start the application in the debugger. Step into each statement, and observe which methods are called during the application. The application displays the following information on the console:
    Name=Freda Smith, balance=100
    $5 charge if balance goes below $25
    Name=Freda Jones, balance=20
    $5 charge if balance goes below $25
    Bank charge: $5

ID articol: 307205 - Ultima examinare: 1 aug. 2008 - Revizie: 1