Difference between Abstract class and Interface in C#.NET


I often encounter the situation to discuss the deference between Interfaces and abstract class when I have been to Interviews. Well, .NET have many new Object Oriented features among those Interfaces and abstract class are important.

Interface in C#:


An Interface is not a class, we can say it an entity or a template that is define by the word Interface. We cannot instantiate the Interface and it is just like implementing the polymorphism. An interface should not contain any code in it only method signature should be there.
Below code snippet describes the Interface declaration in C# code.

public interface IMyInterface
{
int UserId { get; set; }

string Description { get; set; }

string UserPhoneNumber(int phoneNumber);

event EventHandler userStatus;

string this[int userIndex] { get; set; }
}




Abstract Class in C#:


Abstract class is just like as normal class but which cannot be instantiated. It means we cannot create an object for the Abstract class. An abstract class starts with the Keyword Abstract. It contains concrete and abstract members. A concrete method may have the implementation but abstract member should only have the signature.
Below is the sample code snippet to define the abstract class.

public abstract class TestAbstract
{
private int testAbstractId;
public abstract void MyTest(int i);

public abstract string this[int Myindex] { get; set; }

public abstract string MyDescription { get; set; }

public TestAbstract(int testAbstractIdValue)
{
testAbstractId = testAbstractIdValue;
}

public int TestAbstractId
{
get
{
return testAbstractId;
}
set
{
testAbstractId = value;
}
}


}



Difference between Abstract Class and Interface :


Abstract Class:


1) Abstract class is a class which cannot be instantiate.
2) Abstract class may have both concrete and abstract members, i.e we may have the implementation and we may not have the implementation in case of abstract members.
3) Multiple inheritances are not supported in case of Abstract class.
4) It is a kind of contract that enforces all the derived classes to carry on the same hierarchies or standards
5) A class does not implement the multiple abstract classes.

Interface:


1) Interface is a template which cannot be instantiate.
2) Interface should have only abstract members and without implementation.
3) Multiple inheritances are supported in case of Interfaces.
4) To implement the same method in the all derived classes.
5) A Class can call the multiple Interfaces.


Comments

Guest Author: khushi03 Feb 2012

Better approach is been provided for .NET beginners.

Author: PalaniKumar.A20 Feb 2012 Member Level: Gold   Points : 4

Diff between interface and Abstract
1).Interface have only signature. or Interfaces can contain only the signature of a method but no body.
whereas Abstract class have signature and definition both are allow. or An abstract class may contain complete or incomplete methods.
2).Interface have not allow modifier access.
whereas Abstract class are allowed modifier access.
3).Thurogh the Interface we can create the Multiple Inheritance
whereas Abstract class are not allow the Multiple Inheritance.
4).Interface is slower compare Abstract class.
5).By default, all members in Interface is Public
whereas members in Abstract class may have different.
6).An interface cannot provide any code.just the signature means all methods must be abstract
But an abstract class can provide complete, default code and/or just the details but at least at least one method must be abstract
7).A class may inherit several interfaces.
But a class may inherit only one abstract class.
8).An interface only contains method definitions but does not contain any code
An abstract class is a class that can not be instantiated but that can contain code

Author: Sridhar Thota27 May 2015 Member Level: Gold   Points : 7

Difference between abstract class and interface?
Abstract class:
*Abstract class can contain both abstract methods and non abstract methods.
*If a class contains at least one abstract method then that class must be declared as abstract class.
*Abstract class can inherit from another abstract class and can inherit from more than one interface.
*Abstract methods must be implemented in derived class.
*Object cannot be created for abstract classes, reference can be created.

public abstract class myabstract: IA, IB //IA and IB are two interfaces
{
public abstract viod m1(); //abstract method
public void m2() //non abstract method
{
//some logic .
}
}

Interface:
*Interface contain only abstract methods, which are public abstract by default.
*Interface can implement another interface but cannot implement abstract class.
*Object cannot be created for interface, reference can be created.

public interface ISample:IMysample
{
void mymethod();
void mymethod2();
}

Regards

Sridhar.
DNS Member.
"Hope for the best.. Prepare for the worst.."



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: