Changes

Jump to: navigation, search

Understanding C Sharp Abstract Classes

16 bytes removed, 17:44, 22 January 2008
no edit summary
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 subclassderive classes. 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 ==
</pre>
Abstract member functions and properties are also declared using the ''abstract'' keyword. For example to declare an abstract method in our BankAccount Hello class the following code is required:
<pre>
So far we have only looked at ''abstract'' class memebers. As discussed above an abstract member is not implemented in the base class and ''must'' be be implemented in derived classes in order for the class to compile.
Another type of member is a ''virtual'' member. A member defined as ''virtual'' must be implemented in the base class, but may be optionally overrided in the derived class if diferent bavior different behavior is required. For example, the following example implements a ''virtual '' method in the ''Talk'' class:
<pre>
</pre>
If we decide that the default ''goodbye()'' method provided by the Talk class is not suitbale for the requirements of the ''SayHello'' subclass we can simpyl simply implement our own version of the method using the override modifier:
<pre>

Navigation menu