Friday, July 18, 2014

Posted by Anmol Sharma
While both of them acts as base types, there are quite significant differences between abstract classes and interfaces in C#. Few noteworthy points are as follows –

-        -   Abstract classes can have access modifiers whereas interfaces doesn’t have access modifiers

-          - Abstract class can inherit either from another abstract class or another interface while an interface can         inherit only from an interface.

-         -  A class can inherit more than one interface, while a class cannot inherit multiple classes at the same time.

-         -  While interfaces can’t have implementation of any members, abstract classes can have.


   Illustration code
using System;

    public abstract class JIMS
    {
        int RollNo;
        public void print() //abstract class can have implemetations
        {
            Console.WriteLine("This is a print method");
        }
    }

 public interface iJIMS
    {
        /* public void print() // interfaces can not have implementations nor access specifier and it will give an error.
        {                      
            Console.WriteLine("This is a print method");
        }*/
     //int RollNo; // interfaces can not have fields while abstract classes can have 
     void Print();

    }


 public class Program
 {
     public static void Main()
     {
         Console.WriteLine("46geekslab - Difference between abstract classes and Interfaces in c#");
         Console.ReadLine();
     }
 }




0 comments:

Post a Comment