Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference Between Abstract class and Interface with Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 911
    Comment on it

    Hello Friends,

     

    Today, I am going to discuss about an abstract class and an interface.

     

    What are the difference between Abstract class and Interface?

    1. An Abstract is a class but an Interface is not a class. It is an entity. Both abstract class and interface cannot be instantiated (i.e. we cannot create an object of both abstract class and interface).
    2. An Abstract class is defined by "abstract" keyword and Interface is defined by "interface" keyword.
    3. An Abstract class can have the const, members/fields, both declaration and definition of properties, and methods, whereas Interface can have only const, declaration of properties and methods. An Interface cannot have fields.
    4. An Abstract class can have various access modifiers such as abstract, protected, internal, public, virtual, etc. but Interface can have only defined methods as public.
    5. An Abstract class can have constructor declaration which is used to initiate common properties for each derived class automatically (Parent class constructor is called first followed by child class constructor). but an Interface cannot have so.
    6. An Abstract class cannot support multiple inheritance (i.e. a class may inherit only one abstract class), but an Interface supports multiple inheritance (i.e. a class may inherit several interfaces at the same time).

     

    When do you choose interface over an abstract class or vice versa?

    An Abstract class is used to provide default behaviour. When we have implementation same for all the derived classes, then it is better to go for an Abstract class. In Interface, you can have your implementation to any class that implements the interface.

    In other words, you can share implementation for all derived classes in one central place, and avoid code redundancy in derived classes.

     

    Example -

    Abstract class - In this example, the class DerivedClass is derived from an abstract class BaseClass. The abstract class contains an abstract method, AbstractMethod, and two abstract properties, A and B.

    abstract class BaseClass   // Abstract class
    {
        protected int _a = 100;
        protected int _b = 150;
        public abstract void AbstractMethod();   // Abstract method
        public abstract int A { get; }
        public abstract int B { get; }
    }
    
    class DerivedClass : BaseClass
    {
        public override void AbstractMethod()
        {
            _a++;
            _b++;
        }
    
        public override int A   // overriding property
        {
            get { return _a + 10; }
        }
    
        public override int B   // overriding property
        {
            get { return _b + 10; }
        }
    
        static void Main()
        {
            DerivedClass derivedClass  = new DerivedClass();
            derivedClass.AbstractMethod();
            Console.WriteLine("a = {0}, b = {1}", derivedClass.A, derivedClass.B);
        }
    }
    
    // Output: a = 111, b = 161

    Interface - In this example, the interface contains the property declaration and the class contains the implementation. Any instance of a class that implements IPoint has integer properties x and y.

    interface IPoint
    {
       // Property signatures:
       int x { get; set; }
       int y { get; set; }
    }
    
    class Point : IPoint
    {
       // Fields:
       private int _a;
       private int _b;
    
       // Constructor:
       public Point(int x, int y)
       {
          _a = x;
          _b = y;
       }
    
       // Property implementation:
       public int x
       {
          get { return _a; }
          set { _a = value; }
       }
    
       public int y
       {
          get { return _b; }
          set { _b = value; }
       }
    }
    
    class MainClass
    {
       static void Print_Point(IPoint p)
       {
          Console.WriteLine("x={0}, y={1}", p.x, p.y);
       }
    
       static void Main()
       {
          IPoint p = new Point(2, 3);
          Console.Write("My Point: ");
          Print_Point(p);
       }
    }
    
    // Output: My Point: x=2, y=3

    IPoint p = new Point(2, 3);

    In above line of code, we are not creating an instance of the interface - we are creating an instance of something that implements the interface.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: