Difference between Interface and abstract class

Introduction:

Hi everyone, in this article I will explain what is abstract class & Interface, what their purpose and what is the difference between them.

Back Ground:

Before starting I want to give a brief wind up on something like
1. Class: It is a collection of methods, attributes, Properties etc., all the methods in the class are concrete classes (definition already exists).
2. Concrete Methods: A method which contains the implementation.
EX:-
Public void ConcreteMethod()
{
Console.WriteLine(“Method with Implementation”);
}
3. Abstract Methods: opposite of abstract methods,i.e. method which does not have implementation or body.
Ex:-
Public void AbstarctMethod();
4. As we know both the abstract class and interface cannot be instantiated, because these are used for implementing the inheritance (which mean to derive), because of this we can use these by deriving in our class.

Abstract Class:
Definition: This is a class, which is a combination of both concrete and abstract methods (method without any definition)
A class which contains at least one abstract method
Purpose:
1. Can implement the common functionalities as concrete methods and non common functionalities as abstract methods in the same class.
2. Normal class + additional features of abstract methods.

Interface:
Definition: This is a skeleton representation of a class. It is a pure abstract class.
Purpose:
1. This is used to interact between different modules to specify the methods and there signatures.
2. This is used to define the methods and the signatures types of the method while defining the skeleton, so that I will be easier to implement them.
3. This is used to force the user to implement all the methods declared in the interface.
4. For the implementation of multiple inheritance.

Difference between Interface and abstract class

1. As we see abstract class does not show full abstartion, but interface is a pure abstract class as it has only abstract methods.
2. Can implement the Multiple Inheritance with the use of interface.
3. The modifiers for methods in interface can be only public the modifiers for methods in abstract class can be public or protected.
4. One or more interfaces can be implemented only one abstract class can be extended
5. Only an interface can extend another interface, but class can extend an abstract class.
6. Abstract classes should have subclasses else that will be useless..Interfaces must have implementations by other classes else that will be useless
7. Accessibility modifier (Public/Private/internal) is allowed for abstract class. Interface doesn't allow accessibility modifier
8. An abstract class can contain fields, constructors, or destructors and implement properties. An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.

Similarities

1.Both the interface and the abstract class can contain zero or nil abstract methods.

2. In both cases all the abstract methods declared should be implemented/extended respectively


Comments

Author: Jigar28 Nov 2009 Member Level: Gold   Points : 0

Hi,
Manoj

Thanks.

Author: Satheesh14 Jul 2010 Member Level: Silver   Points : 1

Hi,
All the methods declared inside an Interface should be 'abstract', right ?
If correct, please explain something more about first point under Similarities section.

Rgds,
Satheesh

Author: Alex Kumar19 Jul 2010 Member Level: Gold   Points : 0

Thanks

Author: Pavan04 Apr 2012 Member Level: Silver   Points : 0

Well illustrated... Thanks Manoj

Author: Sridhar Thota27 Jun 2015 Member Level: Gold   Points : 6

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 Thota.
DNS Member.
"Hope for the best.. Prepare for the worst.."

Author: chitaranjan mallick28 Jun 2015 Member Level: Gold   Points : 6

Hi,

/// Abstract Class

An abstract class is a special kind of class that cannot be instantiated.
Because abstract class is only to be inherited by sub-classed (Child class ). In other words, it only allows other classes to be inherit but cannot be instantiated.
in Abstract class, child class must and should inherit the the parent class method and property.

A class can implement any number of interfaces but a subclass can at most use only one abstract class

//// Interface

An interface is also a type like class. It is an entity that is defined by the word Interface.

An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.

The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class.

Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.



Hope it will help for uu.

Thanks a lot,
Chitaranjan



  • 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: