25 June 2012

Difference between Abstract class and Interface

What is an Abstract class?

Abstract class is a class that cannot be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes.

Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods. 
Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.

What is an Interface?

                     Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
• Single Class can implement multiple interfaces.
• If a class implements a interface then it has to provide implementation to all its methods.


Following are the differences between abstract and interfaces:-

Abstract Class:
-Abstract class provides a set of rules to implement next class
-Rules will be provided through abstract methods
-Abstract method does not contain any definition
-While inheriting abstract class all abstract methods must be override
-If a class contains at least one abstract method then it must be declared as an “Abstract Class”
-Abstract classes cannot be instantiated (i.e. we cannot create objects), but a reference can be created
-Reference depends on child class object’s memory
-Abstract classes are also called as “Partial abstract classes”
-Partial abstract class may contain functions with body and functions without body
-If a class contains all functions without body then it is called as “Fully Abstract Class” (Interface).


Example for the abstract class is



public abstract alass AbstractStudent
    {
        String Roll
        {
            get;
            set;
        }

        String FirstName
        {
            get;
            set;
        }

        String LastName
        {
            get;
            set;
        }


        Public String GetStudentDetails()
             {

                  // Implementation of Method   
             }

        public String SaveStudentDetails ()
            {
                  // Implementation of Method   
             }

        public abstract String CalculateWage();

    }




Interface:
-If a class contains all abstract methods then that class is known as “Interface”
-Interfaces support like multiple inheritance
-In interface all methods r public abstract by default
-Interfaces r implementable
-Interfaces cannot be instantiated, but a reference can be created
• Abstract classes can have concrete methods while interfaces have no methods implemented.
• Interfaces do not come in inheriting chain, while abstract classes come in inheritance.
 Example for the interface is :


public abstract class AbstractStudent
    {
        String Roll
        {
            get;
            set;
        }

        String FirstName
        {
           get;
           set;
        }

        String LastName
        {
           get;
            set;
        }

        Public String GetStudentDetails()
            {
                 // Implementation of Method  
             }

        public String SaveStudentDetails ()
            {
                  // Implementation of Method  
            }

        public abstract String CalculateWage();

    }



An Interface cannot implement methods.
An abstract class can implement methods. 


An Interface can only inherit from another Interface.
An abstract class can inherit from a class and one or more interfaces. 


An Interface cannot contain fields.
An abstract class can contain fields. 


An Interface can contain property definitions.
An abstract class can implement a property. 


An Interface cannot contain constructors or destructors.
An abstract class can contain constructors or destructors. 


An Interface can be inherited from by structures.
An abstract class cannot be inherited from by structures. 


An Interface can support multiple inheritance.
An abstract class cannot support multiple inheritance.