34,333
edits
Changes
New page: In the preceding chapters we looked in detail at object oriented programming in C# and and also at the concept of class inheritance. In this chapter we will look at the next area of object...
In the preceding chapters we looked in detail at object oriented programming in C# and and also at the concept of class inheritance. In this chapter we will look at the next area of object oriented programming, the ''abstract class''.
== What is a C# Abstract Class? ==
In the examples we have looked at so far in this book we have created classes which could be both instantiated as objects and used as a base class from which to inherit to create a subclass. Often a base class is not intended to be instantiated and is provided solely for the purpose of providing an outline for subclasses. Such a class is known as an ''abstract class''. An abstract class cannot be instantiated as an object and is only provided for the purpose of deriving subclasses.
== Abstract Members ==
A C# abstract class contains ''abstract members'' which define what a subclass should contain. These ''abstract members'' only declare that a member of a particular type is required, it does not implement the member. Implementation of abstract members takes place within the derived class. A subclass which derives from an abstract class and fails to implement abstract methods will fail to compile.
== Declaring a C# Abstract Class ==
Abstract classes are declared using the ''abstract'' modifier in the class declaration:
<pre>
public abstract class BankAccount
{
}
</pre>
Abstract member functions and properties are also declared using the ''abstract'' keyword. For example to declare an abstract method in our BankAccount class the following code is required:
<pre>
public abstract decimal calculateInterest();
</pre>
We now have an abstract class with an abstract method named calculateInterest(). Note that this codce only states that any class derived from BankAccount must implement a method called calculateInterest() which returns a Decimal value. It does not, however, implement the method.
== Deriving from an Abstract Class ==
In order to subclass from an abstract class we simply write code as follows:
<pre>
public class SavingsAccount : BankAccount
{
}
</pre>
We now have a class called ''SavingsAccount'' which is derived from the abstract BankAccount class. The next step is to implement the abstract calculateInterest() method. When implementing abstract members in a derived class the ''override'' modifier must be used. For example:
<pre>
public override decimal calculateInterest()
{
return balance * interestRate;
}
</pre>
== What is a C# Abstract Class? ==
In the examples we have looked at so far in this book we have created classes which could be both instantiated as objects and used as a base class from which to inherit to create a subclass. Often a base class is not intended to be instantiated and is provided solely for the purpose of providing an outline for subclasses. Such a class is known as an ''abstract class''. An abstract class cannot be instantiated as an object and is only provided for the purpose of deriving subclasses.
== Abstract Members ==
A C# abstract class contains ''abstract members'' which define what a subclass should contain. These ''abstract members'' only declare that a member of a particular type is required, it does not implement the member. Implementation of abstract members takes place within the derived class. A subclass which derives from an abstract class and fails to implement abstract methods will fail to compile.
== Declaring a C# Abstract Class ==
Abstract classes are declared using the ''abstract'' modifier in the class declaration:
<pre>
public abstract class BankAccount
{
}
</pre>
Abstract member functions and properties are also declared using the ''abstract'' keyword. For example to declare an abstract method in our BankAccount class the following code is required:
<pre>
public abstract decimal calculateInterest();
</pre>
We now have an abstract class with an abstract method named calculateInterest(). Note that this codce only states that any class derived from BankAccount must implement a method called calculateInterest() which returns a Decimal value. It does not, however, implement the method.
== Deriving from an Abstract Class ==
In order to subclass from an abstract class we simply write code as follows:
<pre>
public class SavingsAccount : BankAccount
{
}
</pre>
We now have a class called ''SavingsAccount'' which is derived from the abstract BankAccount class. The next step is to implement the abstract calculateInterest() method. When implementing abstract members in a derived class the ''override'' modifier must be used. For example:
<pre>
public override decimal calculateInterest()
{
return balance * interestRate;
}
</pre>


