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

Popular posts from this blog

Maximize child MDI form without ControlBox

Adding text to label using javascript

Dynamically creation of web controls and binding Validations Using ASP.NET