Thursday, January 26, 2012

Abstract Class


What is an Abstract class?

Abstract classes, which declared with the abstract keyword, cannot be instantiated. It can only be used as a super-class for other classes that extend the abstract class.
An abstract class can contain either abstract methods or non-abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.

public abstract class AbstractClass
    {

        public  abstract void absMethod(); //should not have implementation. Implementation should be present in the derived class
       
        public void nonAbsMethod()
        { }
       

    }

Purpose

Abstract classes are ideal when implementing frameworks.  The purpose of an abstract class is to provide a common definition of a base class that multiple derived
An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes

Rules of abstract class

A class can inherit only from one abstract class (but a class may implement many interfaces)

A class inheriting from an abstract class must override all its abstract methods/ properties and may override virtual methods/ properties. 

A method cannot be abstract within a non-abstract class. Declaration of abstract methods are only allowed in abstract classes

An abstract class cannot be sealed or static

//Incorrect Code
public abstract  sealed class AbstractClass

Abstract methods

An abstract class can have abstract methods and abstract properties.

Derived classes of the abstract class must implement all abstract methods.implementation in derived class is done by using "override"

    public class MyClass:AbstractClass
    {
        protected override void absMethod()
        {
            //Code logic
        }
    }

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. 

public abstract class AbstractClass
    {
        protected abstract void absMethod();

    }


Rules of abstract methods

An abstract member cannot be static.

//Incorrect Code: A static member cannot be marked as override, virtual, or abstract
public  abstract static void absMethod();

The abstract method cannot be marked virtual

//Incorrect Code
public  abstract virtual void absMethod();

Virtual or abstract members cannot be private

//Incorrect Code
private  abstract void absMethod();

If the access modifier of an abstract method as protected, it should be protected in its derived class

Abstract Properties

Abstract class can have abstract properties & non abstract properties. In derived class, the abstract properties has to be overridden

    public abstract class AbstractClass
    {

        public abstract int pub_var
        {
            get;
            set;
        }
    }

Virtual Member

In abstract class, we can have virtual member. A member defined as virtual must be implemented in the base class, but may be optionally overridden in the derived class if different behavior is required.

public abstract class AbstractClass
    {
        public virtual int pub_var { get; set; }
        
        public virtual void virMtd(){}
    }

You cannot have just get property in abstract class if you define it as virtual. 

public abstract class AbstractClass
    {
       //Incorrect
public virtual int pub_var { get; }
        
    }

You either need to have get & set or implementation to get

public abstract class AbstractClass
    {
        public virtual int pub_var
        {
            get
            {
                return 1;
            }
        }
    }


Abstract class vs. Interface

An abstract class can have abstract members’ as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

When to use abstract class

Say you have 2 classes, and you wanted to implement some similar functionality. Instead of modifying the two classes, you can create an abstract class and have the common functionality in it and have those 2 classes inheriting from the abstract class.
Now, if you need any different functionality in one of the class, then you can override the method for the unique implementation.

The below recommendation is taken from msdn link “Recommendations for Abstract Classes vs. Interfaces: http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class. 

If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Scope of the class

Any class within the namespace should only be public.

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

Partial class

A partial method must be declared within a partial class or partial struct

A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers           

No comments:

Post a Comment