Memi.Reflection

Private members of memi's thoughts

Inherits vs Implemets in C#

I guess the difference is obvious - you inherit a class and implement an interface.

If you write in VB.NET, the difference just shines. Your code looks like:

public class MyClass: Inherits ClassX Implements IInterface.

However, as you sure know, in C# things are a bit messier. The code above will look in C# like this:

public class MyClass : ClassX, IInterface.

I was in a number of presentations where the presenter showed a code silimar to the above, and said the class inherits from IInterface. But the highlight of this was in the (great) TechEd in Israel, when a presenter said, when talking about a code similar to the above, that we have here a multiple inheritance.

Why is it that there is no clear distinction between inherits and implements in C#? Is there any rational reason for this, or the architects just count on the developer to know whether he inherits or implements, based on the capital I?

Posted: May 07 2004, 05:56 PM by memi | with 28 comment(s)
Filed under:

Comments

dj said:

yes, any respectable c# developer should know this. vb.net is way too wordy.
# May 7, 2004 11:06 AM

Matt Hawley said:

Amen to that. However the whole naming convention for interfaces is there for a reason - so developers will know if it is an interface. I'd have a hard time listening to that presenter after the fact, I mean he may know a little, but if he even *thinks* that multiple inheritance is possible, hes way over misinformed and tredding on just plain "HELLO" mode. Makes me almost wonder if he had any experience at all or was just giving the canned talk.
# May 7, 2004 11:13 AM

Christian Nagel said:

With interfaces named with I this is clear anyway.
# May 7, 2004 11:15 AM

denny said:

or....

IMHO VB is better in this area....

there are differences between an interface and a base class.... *BIG* ones!

so say you are having to maintian code someone else wrote.... say they wrote it in C# and were less than stellar at using "I" in interfaces and so on...

you might not realize at first which case you had .....

but in VB the two cases are 100% clear, you know on one (or two) lines what you have.

BTW: I am *NOT* wanting less skills in a dev. just why should we bust our chops on that kind of stuff? I'd rather create a new / better algorithm for something than have to chase down an interface definition....
# May 7, 2004 11:18 AM

Memi Lavi said:

I feel uncomfortable with the assumption that an interface should be distincted from class by the 'I' at the beginning, and that's all.
I really don't think you can base your architecture on code conventions. Convention, unlike architecture, is subjet to changes. If tomorrow the convention will change, and interfaces no longer will begin with 'I' (as in Java) - what will you do then?

BTW - the presenter is far from being a newbie. He is a CTO of a large software company in Israel.
# May 7, 2004 11:20 AM

Christian Nagel said:

Matt, multiple inheritance is possible with interfaces. One interface can inherit multiple interfaces.
interface IA
interface IB
interface IC : IA, IB
# May 7, 2004 11:21 AM

Roy Osherove said:

who was the presenter? what talk was this?
# May 7, 2004 4:54 PM

Memi Lavi said:

Christian - I think you used the wrong terms in your post. Multiple Inheritance is not possible with interfaces. Multiple Implementation is what you're looking for. A class can implement multiple classes, while it can inherit only one class.
The definition of Inheritance is that a class takes all the functionality of its ancestor. Interfaces do not have functionality at all, and therefore you cannot inherit them. Interfaces only define the class, well, interface, and not its inner plumbing, and therefore you Implement it, and not inherit it.

Roy - I really don't think I should tell the presenter name (although he is a CTO of one of our largest competitors...). I don't think it has anything to do with this discussion. If it is really important to you - send me a mail.
# May 8, 2004 1:42 PM

Christian said:

Memi, what do you name the inheritance relationship with an interface that has multiple interfaces it derives from? An interface never has an implementation, so this is not implementation inheritance.
If you have a class that derives from multiple interfaces, of course this is "implements". If you have an interface that derives from multiple interfaces, this is not "implements", you ust "inherit" the members of the base interface.

interface IA
{
void A();
}
interface IB
{
void B();
}
interface IC : IA, IB // this is not an implementation
{
void C();
}
class X : IC // this is an implementation
{
public void A() {}
public void B() {}
public void C() {}
}
# May 9, 2004 12:31 AM

Govinda said:

We can inherit multiple class in interfaces

# June 10, 2007 4:50 AM

Ummm said:

Jeff's comment may be the dumbest thing I've ever read in my life.

# October 30, 2007 3:21 PM

James said:

In C# you can inherit an interface for which the implementation is in a base class.  This cannot be done in VB.  If the Implements keyword had been used in C#, the below code would not make sense since B does not actually provide the implementation for IFoo but inherits it.

   public class A

   {

       public void DoIt()

       {

       }

   }

   public class B:A,IFoo

   {

   }

   public interface IFoo

   {

       void DoIt();

   }

# November 28, 2007 1:47 PM

Maxx said:

Speaking of inheritance, I would hate to be the guy who inherits Jeff's mountain of spaghetti.

# April 23, 2008 2:17 PM

PasserBy said:

Some people have serious personality problems here - too many code gurus are too arrogant.

# September 2, 2008 9:38 PM

ArchitectJustStoppingBy - Derek Hinch said:

Well, all your syntax in either language (C#/VB) reduces itself down to IL, where you can have multiple implementations, but just once Inheritance. Regardless of use, syntax declaration, or otherwise - you are stuck with this fact if you use .Net.

# September 4, 2008 10:04 PM

cptHotKeys said:

You both inherit and implement an interface, an interface has definitions of methods and so on with no functionality or behavior. When we implement an interface we also inherit its behavior and functionality, or lack there of. It is upto the developer to add functionality which makes it more like polymorphism....

# November 1, 2008 5:55 AM

cptHotKeys said:

James, why would you need to declare that the derived class implements the interface when it is already done in the base class.

Does the derived class not inherit the  (for lack of better words) decloration of implementation?

# November 1, 2008 6:03 AM

Derek Hinch said:

cptHotKeys: An interface is a definition of enforcement that describes a class's assured functionality. It can also be used to instaniate a type base on interface where you can cast down another type to be able to interact in a pure faction - between the objects with assurance those function signatures exist as well as the base return types. Take a look at WCF and datacontracts VS. implementation methods. You will see the latter interace usages within WCF. The fore usages simply are used for assured, type safe interaction between two types. If you are not using the 'object sender' style observer pattern event args, you can use generics to know the types, thus evaluate with reflection to parse the Interface implemenations. Once verified, you can be assure a cast to ItheInterface will be assured between your implementer are that sender.

If this doesn't make sense, you suck and Im sorry. I am drunk and am still wondering how this question is still left unanswered.....wtfo, 4chan anyone.

# June 5, 2009 1:57 AM

DreamKiller said:

Jeff sux alot he needs attention shame too bad he looking in the wrong places anyway . . .thats why u all losers r called or self-proclaimed developers (like jeff) to differentiate btwn class n interface do ur job!

# May 8, 2010 6:19 PM

electroprom said:

When I met my real love((((((

# March 26, 2011 4:32 PM

medicadvice said:

Manchester United - Champion!

# April 8, 2011 12:01 PM

Ananrad said:

That's it for this article begin to read this blog. Plus, a subscriber:)

# June 25, 2011 12:41 AM

radiolocate said:

author, continued in the same spirit

# July 27, 2011 6:16 PM

Luiz Oliveira said:

How could such a simple topic become a long discussion?

I agree, in part, with Memi, got his point but in my opinion it´s a bit misguided.

Conventions may change with time and relying on them is prone to architectural barriers.

On the other hand, when working on large projects, a problem even worse emerges when conventions are not used. Imagine a long list of files and oyu not knowing wich are classes or interfaces. You would end up needing a set of organization rules, wich aint that different from a naming convention.

Given that scaleability is gotta be taken seriously even in the smallest of the projects, I vote for naming conventios on C# and think it's way better than the wordy VB.

# February 27, 2013 9:16 AM