Abstract Classes

Each type of Control has a different shape and appearance. Drop-down ListBoxes look very different from Buttons. Clearly, every subclass of Control should implement its own DrawControl( ) method—but so far, nothing in the Control class enforces that they must do so. To require subclasses to implement a method of their base, you need to designate that method as abstract, rather than virtual.

An abstract method has no implementation. It creates a method name and signature that must be implemented in all derived classes. Furthermore, making at least one method of any class abstract has the side effect of making the entire class abstract.

Abstract classes establish a base for derived classes, but it is not legal to instantiate an object of an abstract class. Once you declare a method to be abstract, you prohibit the creation of any instances of that class.

Thus, if you were to designate DrawControl( ) as an abstract method in the Control class, the Control class itself would become abstract. Then you could derive from Control, but you could not create any Control instances. That makes sense, because the Control class is an abstraction—there is no such thing as a simple Control object, only objects derived from Control.

Making Control.DrawControl( ) abstract means that each class derived from Control would have to implement its own DrawControl( ) method. If the derived class failed to implement the abstract method, that derived class would also be abstract, and again no instances would be possible.

You designate a method as abstract simply by placing the abstract keyword at the beginning of the method definition:

abstract public void DrawControl( );

(Because the method can have no implementation, there are no braces, only a semicolon.)

If one or more methods are abstract, the class definition must also be marked abstract, as in the following:

public abstract class Control

Example 11-3 illustrates the creation of an abstract Control class and an abstract DrawControl( ) method.

Example 11-3. Abstract methods form a contract so that all derived classes must implement their own versions of the method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example_11_3_ _ _ _Abstract_Methods
{
    public abstract class Control
    {
        protected int top;
        protected int left;

        // constructor takes two integers to
        // fix location on the console
        public Control(int top, int left)
        {
            this.top = top;
            this.left = left;
        }

        // simulates drawing the control
        // notice: no implementation
        public abstract void DrawControl( );

    }     // end class Control

    // ListBox derives from Control
    public class ListBox : Control
    {
        private string listBoxContents; // new member variable

        // constructor adds a parameter
        public ListBox( int top, int left, string contents)
                       : base(top, left) // call base constructor
        {
            listBoxContents = contents;
        }

        // an overridden version implementing the
        // abstract method
        public override void DrawControl( )
        {
            Console.WriteLine("Writing string to the listbox: {0}",
                              listBoxContents);
        }
    }     // end class ListBox

    public class Button : Control
    {
        public Button( int top, int left) : base(top, left) { }

        // override the abstract method
        public override void DrawControl( )
        {
            Console.WriteLine("Drawing a button at {0}, {1}
",
                              top, left);
        }
    }        // end class Button

    public class Tester
    {
        static void Main( )
        {
            Control[] controlArray = new Control[3];
            controlArray[0] = new ListBox(1, 2, "First ListBox");
            controlArray[1] = new ListBox(3, 4, "Second ListBox");
            controlArray[2] = new Button(5, 6);

            for (int i = 0; i < 3; i++)
            {
                controlArray[i].DrawControl( );
            }     // end for loop
        }        // end main
    }           // end class Tester
}

The output looks like this:

Writing string to the listbox: First ListBox
Writing string to the listbox: Second ListBox
Drawing a button at 5, 6

In Example 11-3, the Control class has been declared abstract and therefore cannot be instantiated. If you replace the first array member:

controlArray[0] = new ListBox(1,2,"First ListBox");

with this code:

controlArray[0] = new Control(1,2);

the program generates the following error at compile time:

Cannot create an instance of the abstract class or interface 'Control'

You can instantiate the ListBox and Button objects because these classes override the abstract method, thus making the classes concrete (that is, not abstract).

Often, an abstract class will include nonabstract methods. Typically, these will be marked virtual, providing the programmer who derives from your abstract class the choice of using the implementation provided in the abstract class, or overriding it. Once again, however, all abstract methods must, eventually, be overridden to make an instance of the (derived) class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset