Frank said:
We are having one of those religious debates at work: Interfaces vs
Classes.
My take is that Classes give you more flexibility.
Well, there are two dimensions of flexibility here: for the definer and
the user of the interface/class.
All the languages i know that distiguish between interfaces and classes
(C#, JAVA, ...not C++) have this distinction because they don't want to
have the multiple-inheritance problem with offset-based member-fields.
In short you can mutiple implement interfaces and singularly inherit
classes.
The multiple-inheritance problem does not prevent interfaces from
actually defining code, which unfortunately none of C# or JAVA allows.
So a class allows you to specify static- and member-fields (and method
content), giving the definer flexibility.
Using an interface allow implementation to still inherit and to
implement other interfaces, giving the implementer flexibility.
Obviosly, for actual implementation a class is needed.
Other than that, my opinion is that neither classes nor interfaces
should be "generally preferred" when passing data around, since they are
both valuable assets in programming with respective strengths and
weaknesses. You need to choose based on the situation, not a "general rule".
You can
enforce a contract on the descendant classes by marking methods abstract
(which is all that an interface does).
So, If that's what you need, use an interface (if it's not too much
code), otherwise use a class.
Remember that you can reuse code by other means than inheritance,
compostion works fine (especially in garbage-collected languages). You
can also define helper-functions which implementers can use.
In addition, the classes allow
you to be flexible by adding functionality that child classes inherit.
This is valuable if all implementations shares a way of coding... or if
there is only 1
But, you could easily put that code in a helper-class and allow other
the choice of whether to use the helper-class.
interface Foo { void f(int i); }
class DefaultFoo implements Foo { void f(int i) { /* impl. */ } }
But remember, less lines of code also means less bugs, so use the
construct judiciously
Or by providing optional functionality via virtual methods.
Optional? you specificly mean optionally overridden, right?
The same construct as above applies.
Now, I understand that Interfaces have its place, but I am having a hard
time finding a situation where the classes (via inheritance) do not
provide an identical or better solution.
If you are in C# (or JAVA): Multiple implementation, it's the one and
only thing that interfaces can do that classes cannot.
You can argue all day about whether it's a good or bad idea, but it is
often needed to bridge two colliding conceptions of the same thing...
*very* often frameworks give you this problem.
If you do cross-language integration using .NET, for example using COM,
you should know that some tools only map interfaces between languages.
Am I missing something? I am open to arguments from both sides.
Well, multiple implementation is a valuable thing to be able to do.
Besides, there are many ways to workaround the problem that interfaces
cannot specify method-code.
I think you really just have to accept that neither interfaces nor
classes are superfluous and you need to be able to decide in a
case-by-case manner which one to use in order to minimize work and
maximize usability.