Understanding C Sharp Abstract Classes

From Techotopia
Revision as of 15:12, 2 April 2009 by Neil (Talk | contribs)

Jump to: navigation, search
PreviousTable of ContentsNext
C# InheritanceIntroducing C# Arrays


<google>BUY_CSHARP_ESSENTIALS</google>


In the preceding chapters we looked in detail at object oriented programming in C#, 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.


Contents


What is a C# Abstract Class?

<google>ADSDAQBOX_FLOW</google> 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 derive 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

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:

public abstract class Talk
{
}

Abstract member functions and properties are also declared using the abstract keyword. For example to declare an abstract method in our Talk class the following code is required:

     public abstract void speak();

We now have an abstract class with an abstract method named speak(). Note that this declaration only states that any class derived from the Hello base class must implement a method called speak() which returns no value (i.e it is declared as void). 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:

public class SayHello : Talk
{
}

We now have a class called SayHello which is derived from the abstract Talk class. The next step is to implement the abstract speak() method. When implementing abstract members in a derived class the override modifier must be used. For example:

public override void speak()
{
	Console.WriteLine("Hello!");
}

We now have a subclass derived from the Talk abstract class which implements the abstract speak() method.

We can now bring all of this together into a simple program:

using System;

class Hello
{


public abstract class Talk
{
	public abstract void speak();

}

public class SayHello : Talk
{
	public override void speak()
	{
		Console.WriteLine("Hello!");
	}
}

        static void Main()
        {
		SayHello hello = new SayHello();
		
		hello.speak();
        }
}

The Difference Between abstract and virtual Members

So far we have only looked at abstract class members. As discussed above an abstract member is not implemented in the base class and must 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 overriden in the derived class if different behavior is required. For example, the following example implements a virtual method in the Talk class:

using System;

class Hello
{


public abstract class Talk
{
	public abstract void speak();


	public virtual void goodbye()
	{
		Console.WriteLine("Talk class says goodbye!");

	}
}

public class SayHello : Talk
{
	public override void speak()
	{
		Console.WriteLine("Hello!");
	}
}

        static void Main()
        {
		SayHello hello = new SayHello();
		
		hello.speak();
		hello.goodbye();
        }
}

When executed, the goodbye() method in the Talk class will be executed resulting in the following output:

Hello!
Talk Class says goodbye!

If we decide that the default goodbye() method provided by the Talk class is not suitable for the requirements of the SayHello subclass we can simply implement our own version of the method using the override modifier:

using System;

class Hello
{


public abstract class Talk
{
	public abstract void speak();


	public virtual void goodbye()
	{
		Console.WriteLine("Talk class says goodbye!");

	}
}

public class SayHello : Talk
{
	public override void speak()
	{
		Console.WriteLine("Hello!");
	}

	public override void goodbye ()
	{
		Console.WriteLine ("SayHello Class says goodbye!");
	}
}

        static void Main()
        {
		SayHello hello = new SayHello();
		
		hello.speak();
		hello.goodbye();
        }
}

Now when we execute our program the SayHello implementation of the googbye() method will be called:

Hello!
SayHello Class says goodbye!


<google>BUY_CSHARP_ESSENTIALS</google>



PreviousTable of ContentsNext
C# InheritanceIntroducing C# Arrays