C Sharp Inheritance

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
C# Object Oriented ProgrammingUnderstanding C# Abstract Classes


Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99

Buy eBook


In C# Object Oriented Programming we looked at the basics of object oriented programming in C#. Now that we have covered these basics the next topic to be covered is that of class inheritance.




What is Inheritance?

The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined which has a number of characteristics and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.

By deriving classes we create what is often referred to as a class hierarchy. The class at the top of the hierarchy is known as the base class and the derived classes as subclasses. Any number of classes may be derived from a class. It is only possible for a derived class to inherit from one class. As such, C# is known as a single inheritance programming language.

Classes need not only be derived from a base class. For example, a subclass can also inherit from another subclass.

An Example of Inheritance

As with most programming concepts the subject of inheritance in C# is perhaps best illustrated with an example. In the previous chapter we created a class called BankAccount:

public class BankAccount 
{

	public string accountName;
        public int accountFee;
	private int accountBalance;
	private int accountNumber;

	public int getAccountNumber()
	{
		return accountNumber;
	}

	public void setAccountNumber(int newNumber)
	{
		accountNumber = newNumber;
	}

}

This class does a good job of defining characteristics common to any type of bank account, such as account holder name, account number and current balance. Imagine, however, that our banking program needs to support a number of specific types of account. For example, the bank might offer its customers an interest bearing savings account. A savings account will have all the characteristics of our BankAccount class but would also need a way to store the prevailing interest rate. One option would be to create a brand new class from the ground up called SavingsAccount which duplicates everything we have in our BankAccount class, plus extra members needed for a savings account. Another, more efficient method is to derive a SavingsAccount class from the BankAccount class and then add in the extra functionality into this subclass.


Creating a Subclass in C#

Now that we have ascertained that we need to create a sub class of our BankAccount class we can take a look at the code necessary to achieve this. Subclasses are declared in the same way as any other class with the exception that the class name is followed by a colon (:) followed by the name of the class from which it is to inherit. With this in mind we can begin by creating our SavingsAccount class:

public class BankAccount 
{

	public string accountName;
	public int accountBalance;
	public int accountNumber;

        public BankAccount (string name, int number)
	{
		accountName = name;
		accountNumber = number;
        }

	public int getAccountNumber()
	{
		return accountNumber;
	}

	public void setAccountNumber(int newNumber)
	{
		accountNumber = newNumber;
	}

}

public class SavingsAccount : BankAccount
{
}

We have now created a sub class of BankAccount called SavingsAccount, but at this point the SavingsAccount class is no different than its parent class. Next we need to add some new members to add the behavior we need:

public class SavingsAccount : BankAccount
{
        public double interestRate;

        public SavingsAccount (string name, int number, int balance, double rate)
		: base (name, number)
	{
		accountBalance = balance;
		interestRate = rate;
        }

	public double monthlyInterest()
	{
		return interestRate * accountBalance;
	}
}

We now have a new class called SavingsAccount which inherits all the members of the BankAccount class and adds some members of its own. In particular we have added a new data member called interestRate which will store the interest rate paid on the account together with a new method to calculate the monthly interest.

Passing Arguments to the Base Class Constructor

Of particular significance is the constructor. In the BankAccount base class we have a constructor which takes the account name and account number as arguments. In the SavingsAccount subclass we need to accept two additional arguments - the balance and the interest rate. The : base code instructs C# to handle the name and number arguments using the constructor from the base class. The remaining two arguments are then passed to the SavingsAccount constructor.

With our subclass complete we can now make use of it:

       static void Main()
        {
	  SavingsAccount saveAccount = new SavingsAccount("Fred Wilson", 123456, 432, 0.02F);

 	  Console.WriteLine ("Interest this Month = " + saveAccount.monthlyInterest() );
        }

This in essence is what inheritance is all about. In the next chapter we will begin looking at Understanding C# Sharp Abstract Classes


Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99

Buy eBook



PreviousTable of ContentsNext
C# Object Oriented ProgrammingUnderstanding C# Abstract Classes