Inheritance

Inheritance is one of the most important, and most powerful, of the object-oriented programming concepts. Using inheritance, one class can be derived from another. The derived class, also known as the child class or subclass, inherits functionality from the base class, also known as the parent class or superclass. The subclass can add methods and properties or modify the functionality that it has inherited to provide a more specialised version of the base class.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. C# is known as a single inheritance programming language. Using inheritance, classes become grouped together in a hierarchical tree structure. The more generalised classes appear at the root of the hierarchy with the more specific classes appearing on the tree's branches. This categorisation of classes into related types is why inheritance is sometimes referred to as generalisation

An Example of Inheritance

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
{
}

Here we created a class called BankAccount.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#

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 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

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() );
}

0 comments:

Template by - Mathew | Mux99