C Sharp Inheritance

From Techotopia
Revision as of 20:28, 18 January 2008 by Neil (Talk | contribs) (New page: In C# Object Oriented Programming we looked at the basics of object oriented programming in C#. Now that we have covered these basics the topic to b...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In C# Object Oriented Programming we looked at the basics of object oriented programming in C#. Now that we have covered these basics the 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 sub classes. 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 sub class can also inherit from another sub class.

An Example of Inheritance

As with most programming concepts the subject 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 defined characteristics common to any type of bank account, such as account holder name, account number and current balance. Imagine, however, that our backing program needs to support a number of specific types of account. For example, the bank might offer it's 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 sub class.


Create a Sub Class 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. Sub classes 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 accountFee;
	private int accountBalance;
	private int accountNumber;

	public int getAccountNumber()
	{
		return accountNumber;
	}

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

}

public class SavingsAccount : BankAccount
{
        int interestRate;
}

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: