Sunday 22 April 2012

Difference between Abstract class and interface. || what is abstract class || what is interface || Features of Abstract Classes and interfaces

Hi friends, in this article i will explain about the Difference between Abstract class and interface or what is abstract class or what is interface or Features of Abstract Classes and interfaces.
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 behaviour. 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.

Simple Example :




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 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();

    }

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();

    }

Simple Example:

Conclusion:
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. 


 
 
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."  
 
 
 
 
 
 
 

4 comments:

  1. Good, give any example code for this artical.

    Thanks,
    Raja

    ReplyDelete
    Replies
    1. Hi Raja,Thanks fro your suggestion
      i will add the code ....

      Delete
    2. Nice Example for Difference between Abstract and Interface

      Delete

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.