Click here to Skip to main content
11,570,304 members (50,945 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
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 14-Feb-11 21:39pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
AlbinAbel at 15-Feb-11 3:58am
   
good answer, my 5
maruthimaru at 3-Apr-14 3:42am
   
Very Good Answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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).
Smile | :)
  Permalink  
Comments
SAKryukov at 15-Feb-11 22:28pm
   
This is an answer of my choice so far, my 5.
-SA
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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:

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
  Permalink  
Comments
CPallini at 16-Feb-11 2:55am
   
Good. My 5.
SAKryukov at 16-Feb-11 14:37pm
   
I knew you would understand the subtlety, thank you :-)
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here[^] is a discussion which might help or at least get you started.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 15 Feb 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100