Tuesday, July 15, 2014

Abstract classes

Abstract classes
A class defined as abstract is used as a base class. Such a class is used for the purpose of inheritance only i.e. other classes are derived from this class. We cannot create an object of an abstract class.
The syntax of an abstract class is:

abstract class class-name
{
// members of the class
}
 
Implementation:
Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.
In some cases, abstract classes constitute the top few levels of the hierarchy, for Example abstract class Shape with abstract method Draw() has tow derived abstract classe Shape2D & Shape3D inherites the method Draw() & also do not provide any implementation for it. Now we have normal classes Rectangle, Square & Circle inherites from Shape2D, and another group of classes Sphere, Cylinder & Cube inherites from Shape3D. All classes at the bottom of the hierarchy must override the abstract method Draw().
A class is made abstract by declaring it with Keyword abstract.

    public abstract class Shape
    {
        //...Class implementation
        public abstract void Draw(int x, int y)
        {
            //this method mustn't be implemented here.
            //If we do implement it, the result is a Syntax Error.
        }
    }
    public abstract class Shape2D : Shape
    {
        //...Class implementation
        //...you do not have to implement the the method Draw(int x, int y)
    }
    public class Cricle : Shape2D
    {
        //here we should provide an implemetation for Draw(int x, int y)
        public override void Draw(int x, int y)
        {
            //must do some work here
        }

     }



Abstract Properties:                          
Following is an example of implementing abstract properties in a class.

    //Abstract Class with abstract properties
    abstract class absClass
    {
        protected int myNumber;
        public abstract int numbers
        {
            get;
            set;
        }
    }
    class absDerived : absClass
    {
        //Implementing abstract properties
        public override int numbers
        {
            get
            {
                return myNumber;
            }
            set
            {
                myNumber = value;
            }
        }
    }
 
In the above example, there is a protected member declared in the abstract class. The get/set property for the member variable myNumber is defined in the derived class absDerived.
Some points to remember:
  1. An abstract class cannot be a sealed class.
  2. An abstract method cannot be private.
  3. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
  4. An abstract member cannot be static.
  5. The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
Difference between an abstract method & virtual method:
Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
Summary:

We have seen how to use abstract class in c#.Abstract class is a class that has no direct instances, but whose descendants may have direct instances.

No comments :

Post a Comment