Saturday, July 19, 2014

Difference Between Abstract Class and an Interface

INTRODUCTION :
In this article I am going to explain the difference between an Abstract Class and an Interface with some examples.
DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE:
  1. An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
  2. Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
  3. We can not declare a member field in an Interface.
  4. We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
  5. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
A) Abstract Class : We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.
Take a look at an Abstract class example:
FIG3.jpg
The Code window looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
   abstract
 class M1
   
{
        public int add(int a, int b)
        {
            return (a + b);
        }
    }
    class M2 :M1   

    {
        public int mul(int a, int b)
        {
            return a * b;
        }
    }
    class test   

    {
        static void Main(string[] args)
        {
            M2 ob = new M2();
            int result = ob.add(10, 20);
            Console.WriteLine("the result is {0}", result);
            Console.ReadLine();
        }
    }
}
When we run it the output looks like this:

fig1.jpg

B) An Interface : The syntax of an Interface looks like this:
interface
{
     //Interface member
}
NOTE :
  1. An Interface member can not contain code bodies.
  2. Type definition members are forbidden.
  3. Properties are defined in an interface with the help of an access block get and set, which are permitted for the property.
          e.g.   Interface myInterface
                           {
                              int myint 
                                {
                                     get;
                                     set;
                                  }
                             }
Take a look in an interface example:

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

namespace ConsoleApplication3
{
    interface
 MyInterface
    
{
      void
 myMethod();
    }
    class
 MyClass : MyInterface
    
{
      public
 static void Main()
      {
            MyClass
 cls = new MyClass();
            cls.myMethod();
      }
       public
 void myMethod()
      {
            Console.WriteLine("
welcome to MCN IT SOLUTION");
            Console.ReadLine();
       }
    }
}
After running this program we obtain output as follows:
fig2.jpg

NOTE :  In C# , an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.

Related Posts:

  • Http is stateless, What does this mean? Ans: Stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of reque… Read More
  • What is Session ID in Asp.net? Ans: Asp.Net use 120 bit identifier to track each session. This is secure enough and can't be reverse engineered. When client communicate with server, only session id is transmitted, between them. When client request for dat… Read More
  • What are the Advantage and disadvantage of Session? Ans: Advantages:Session provide us the way of maintain user state/data.It is very easy to implement.One big advantage of session is that we can store any kind of object in it. :eg, datatabe, dataset.. etcBy using session we … Read More
  • What is Session? Ans: We know that Http is stateless, means when we open a webpage and fill some information and then move to next page then the data which we have entered will lost.It happed do to Http protocol stateless nature. So here ses… Read More
  • By default where the sessions ID's are stored ? Ans: By default, the unique identifier for a session is stored in a non-expiring session cookie in the browser. You can specify that session identifiers not be stored in a cookie by setting the cookieless attribute to true i… Read More

0 comments:

Post a Comment