TopMenu

Abstract class vs Interface in OO

Background

Object Oriented Programming conceptsRecently I answered a question asking about the difference in Abstract class and Interface. I gave him an explained answer but had this Idea to share it with everyone. This was not the very first question and asked many time on many forums then I looked at the answers. Those have the same articles blogs links and explanation about differences in both these terms from C# implementation view and not actually by Object oriented point of view. So this blog is the same content I provided in answer but had this thought to share it with more audience. 

The differences between Interfaces and Abstract classes can be categorized by concept and implementation.

From Object oriented view

As you might be knowing the basic principles of object oriented language.

- Abstraction/Encapsulation

- Inheritance

- Polymorphism

Interface

----------------

Interfaces provides high level of abstraction.

They also known as contracts in some design principles of software system.

Decoupled system uses Interfaces to expose dependencies because they represent declarations and not actual implementations. Because Interfaces abstracts implementation besides interface can have multiple definitions represented by multiple classes those implements those interfaces. So they are important part of a software system design which is(should be) less coupled and more cohesive.

Abstract class

----------------

Abstract classes represents a level of abstraction and high level of information sharing also known as Inheritance. Interfaces are not for data sharing at all.

Abstract classes are to define default behavior of any entity(class). It also allows to define abstract and virtual actions. Those actions represents derived/child class implementation. So polymorphism comes in picture. Which interfaces doesn't represent in any way.

When you being learning object oriented system or language its remains confusing in beginning but when you spent years practicing and learning the same, you realize that there's not much to compare for differences because these two things exists for their own purpose.

Difference from C# perspective

C# is a language which follows Object oriented principles. C# doesn't support multiple inheritance which make it close to pure object oriented language. Since C++ has this feature and we learn C++ as first class language during our college time so we compare features. Then question comes how the multiple inheritance works in C#. From many source you learn that there's interface that allows something like multiple inheritance but here the wrong information start crawling into mind and people starts comparing Abstract classes and Interfaces. Interface doesn't have Information sharing i.e. Inheritance feature. They're contracts which declares rules/actions which any class should implement in order to fulfill the contract rules(explained in simple words).

What are the difference in these two terms in C#?

There's plenty of blogs/articles and other source which have mentioned the differences in C# language. So just type it in google. Anyways If you have reached this blog means you might have already gone through those blogs and might be looking for some additional information than what’s out there.

The difference is by concept of OOP and not just in C#. So the above explanation applied to any language which follows OOP and not just C#.

Happy Learning!!!

(Used image source code.tutsplus.com)

No comments:

Post a Comment