Changes

Jump to: navigation, search

Understanding C Sharp Abstract Classes

14 bytes removed, 16:02, 1 April 2009
no edit summary
<pre>
public abstract class HelloTalk
{
}
</pre>
Abstract member functions and properties are also declared using the ''abstract'' keyword. For example to declare an abstract method in our Hello Talk class the following code is required:
<pre>
public abstract void sayHellospeak();
</pre>
We now have an abstract class with an abstract method named sayHellospeak(). Note that this declaration only states that any class derived from the ''Hello'' base class must implement a method called sayHellospeak() which returns no value (i.e it is declared as ''void''). It does not, however, implement the method.
== Deriving from an Abstract Class ==
</pre>
We now have a subclass derived from the ''Talk'' abstract class which implments implements the abstract ''speak()'' method.
We can now bring all of this together into a simple program:
== The Difference Between abstract and virtual Members ==
So far we have only looked at ''abstract'' class memebersmembers. 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 overriden in the derived class if 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 suitable for the requirements of the ''SayHello'' subclass we can simply implement our own version of the method using the override modifier:
<pre>

Navigation menu