Difference between revisions of "C Sharp Inheritance"
m (Text replacement - "<google>BUY_CSHARP_ESSENTIALS_BOTTOM</google>" to "<htmlet>csharp</htmlet>") |
m (Text replacement - "<google>BUY_CSHARP_ESSENTIALS</google>" to "<htmlet>csharp</htmlet>") |
||
Line 8: | Line 8: | ||
− | < | + | <htmlet>csharp</htmlet> |
Revision as of 22:04, 1 February 2016
Previous | Table of Contents | Next |
C# Object Oriented Programming | Understanding C# Abstract Classes |
Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
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; } }
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 |
Previous | Table of Contents | Next |
C# Object Oriented Programming | Understanding C# Abstract Classes |