Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello All,

I am a software developer on Microsoft technologies and having 4 years of experience in C#, VB.NET, Win forms & Windows Service Application development. I am raising a the above question and I know that we can place the concrete implementation of methods in abstract class but we can't implement in an interface and abstract class comes in inheriting chain and Interface in implementation but my question is how we choose one of them and what is the advantage & disadvantage between interface abstract class.
Posted

I could have written it myself but then why to go along all the way when the same thing is already told by lots earlier, look here: Google search result for the same[^]

Though to start with, one of the reasons:
If you are in a situation where your Interface method implementation is same for all the inherting classes then you don't need interface, instead Abstract class should had been used.
When you want something to be followed by every class (must have) and there implementation would differ or not defined then we go for Interface.
 
Share this answer
 
Comments
Albin Abel 15-Feb-11 3:58am    
good answer, my 5
maruthimaru 3-Apr-14 3:42am    
Very Good Answer
Rule of thumb:
If most of the derived classes really would take advantage of inheriting common behaviour then use the abstract base class, otherwise choose the interface (if you're building a library then use the interface).
:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 22:28pm    
This is an answer of my choice so far, my 5.
-SA
Sergey Alexandrovich Kryukov 15-Feb-11 22:52pm    
Please see one extra rule of thumb (one can have a bit of trouble to understand how it differs from yours, sorry, needs some thinking).
--SA
The best answer here is the one by C Pallini.

It needs one subtle recommendation: do not use abstract classes where interface can be used.

It looks like the inference from the rule of thumb offered by C Pallini, but it is not.
Sometimes, you need a common behavior for derived classes which you can use polymorphous container. Interface cannot be used here, because late binding is involved. At the same time, you can use the same very abstract class to express a common interface to all of the classes to be passed, say, as a parameter to some other class not related to your hierarchy.

Event if the public/internal set of features matches your goal — don't do it! Why? In this way you would cut yourself from extra flexibility: ability to create another class not related to your existing hierarchy implementing the same interface.

One could also create a "poor virtual" abstract class (a class with method-only members; all of them being abstract), as a logical equivalent of interface (this is how C++ interfaces are implemented). Same thing: don't do it! Interfaces are interfaces: they add extra isolation from implementation. Consider the following example:

C#
IMyInterface {
    void MethodA();
    void MethodM();
}

class MyInterfaceImplementation : IMyInterface {
    public void MethodA() { /*...*/ } //DO NOT prefer this syntax
    void IMyInterface.MethodB() { /*...*/ } //DO prefer this syntax
}

//... why such preference? Let's see:

MyInterfaceImplementation impl = new MyInterfaceImplementation();
IMyInterface intrfHandle = impl; //so far so good
intrfHandle.MethodA(); intrfHandle.MethodB(); //intended use

impl.MethodA(); //allowed, but not for good: unintended use
//will not compile:
//impl.MethodB(); //not accessible from class handle, needs interface handle


The fact that last (commented out) call will not compile is really good, it help isolation of class instances represented by interfaces from implementations, another reason to prefer "interface for interfacing".

—SA
 
Share this answer
 
Comments
CPallini 16-Feb-11 2:55am    
Good. My 5.
Sergey Alexandrovich Kryukov 16-Feb-11 14:37pm    
I knew you would understand the subtlety, thank you :-)
--SA
Here[^] is a discussion which might help or at least get you started.
 
Share this answer
 
 
Share this answer
 
Let me try to give an example There is an animal class. You have a move function common to all childs. So you have it in the abstract class and implemented in different child classes like cat, dog etc.., Now say you want a jumping dog, a crawling dog, a swimming dog. oh! you will be ending up overriding move function and create 100s of child classes.

Instead have an interface sa IMove which has a move function.

Implement that move function in different classes like

class Jump:IMove , class Swimm:IMove, class Crawl:IMove (behaviour separation)

Then in your dog class have property say public IMove myMove(){ get; set;}

set the myMove from the caller (dependency Injection/ strategy pattern) For example this.myMove=new Crawl();

In the move function call this.myMove.Move();

All these together called composite pattern. If interested google why to use composite pattern
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900