HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Differences between abstract classes and interfaces with example code snippets
Interfaces can contain only member declarations no implementations where as an abstract class can contain abstract members as well as implemented members.

using System;

interface ExampleInterface

{

    void Display();

    //Error: Interfaces cannot have method definition only declaration

    //void Show()

    //{

    //    Console.WriteLine("This is an error");

    //}

}

 

abstract class Class1

{

    //Abstract class can contain abstract members

    //as wel as implemented members

    public abstract void Method1();

    public void MethodImplemented()

    {

        Console.WriteLine("Hello PRAGIM Technologies");

    }

}

Interfaces cannot contain fields, constructors and destructors where as an abstract class can contain fields, constructors and destructors.

using System;

interface ExampleInterface

{

    //Error: Interfaces cannot have fields

    //int i;

    //public ExampleInterface()

    //{

    //    Error: Interfaces cannot have constructors

    //}

    //~ExampleInterface()

    //{

    //    Error: Interfaces cannot have destructors

    //}

 

}

abstract class Class1

{

    //Abstract class can contain fields, constructors and destructors

    int i;

    public Class1()

    {

    }

    ~Class1()

    {

    }

}

A class cannot inherit from multiple abstract classes at the same time where as a class can inherit from multiple interfaces at the same time. Hence classes or abstract classes cannot support multiple inheritance. The only way to support multiple inheritance is thru interfaces.

using System;

interface ExampleInterface1

{

    void Display1();

    void Show1();

}

interface ExampleInterface2

{

    void Display2();

    void Show2();

}

 

abstract class Class1

{

    abstract public void Hide1();

}

 

abstract class Class2

{

    abstract public void Hide2();

}

 

//Error: MainClass cannot inherit from multiple classes

//at the same time.

//class MainClass : Class1, Class2

//{

//    public static void Main()

//    {

//        Console.WriteLine("Hello");

//    }

//}

//A class can inherit from multiple interfaces at the same time

class MainClass : ExampleInterface1, ExampleInterface2

{

    public static void Main()

    {

        Console.WriteLine("Hello");

        //MainClass must implement Display1(),Display2(),Show1() and Show2() methods.

    }

}

Interface members are public by default and does not allow explicitly specyifying access modifiers(private,public,internal,protected and protected internal). An abstract class members can use access modifiers.

interface ExampleInterface

{

    //Error : Interface members cannot use access modifiers.

    //public void Display();

}

abstract class Class1

{

    //Abstract class members can use access modifiers.

    abstract public void Hide1();

}

A class implementing an interface must provide implementation for all the interface members. A class inheriting from an abstract class can either implement all base class abstract members or the class can be marked as abstract.

In the example below
  • Class2 is inheriting from an abstract class Class1 and an interface ExampleInterface
  • class2 implements only the inherited interface Display() method and not Hide1() method. So class2 is marked as abstract. so this proves that a class inheriting from an abstract class can either implement all base class abstract members or the class can be marked as abstract.

using System;

interface ExampleInterface

{

    void Display();

}

abstract class Class1

{

    abstract public void Hide1();

}

abstract class Class2 : Class1, ExampleInterface

{

    public void Display()

    {

        Console.WriteLine("Display() Method implemented");

    }

}

class MainClass : Class2

{

    public override void Hide1()

    {

        Console.WriteLine("Hide1() Method implemented");

    }

    public static void Main()

    {

        Console.WriteLine("Hello");

    }

}

Further Reading
Other related articles

Njoy Programming
   ByPrasad Cherukuri