Object Oriented Specific Question

R

rhaazy

Why would I ever use an interface when I could use an abstract class?

Assuming the only limitation is only having one base class, would this
change assuming you could implement multiple inheritance (and
guarantee not creating ambiguity issues)?

Please share your thoughts or explain something to me if I seem to be
confused.
 
N

Nicholas Paldino [.NET/C# MVP]

Well, in .NET, the fact that you can have only one base class means that
you are better off (usually) using multiple interface implementations, as
you don't want to "burn the base class" (because the functionality that the
implementer wants to expose might be buried somewhere that is not accessible
to the abstract class or classes that derive from it).

If you could implement multiple inheritance, then it wouldn't be such an
issue, but whether or not it would be productive, or cause more problems
than it solves is a different conversation completely.
 
J

Jon Skeet [C# MVP]

rhaazy said:
Why would I ever use an interface when I could use an abstract class?

Assuming the only limitation is only having one base class, would this
change assuming you could implement multiple inheritance (and
guarantee not creating ambiguity issues)?

That's not the only limitation, although it's a significant one. (C#
just *doesn't* support multiple base classes, and is unlikely to ever
do so). It's also easier to mock out interfaces than classes - it tends
to be better supported in tools.
 
B

Brian Rasmussen [C# MVP]

Nicholas and Jon have already pointed out the most important reasons, but I
would like to add that interfaces allow for a more flexible design as the
same interface can be implemented by any class. If you use an abstract class
you're forced to use inheritance in order to supply instances of that type.
In other words interfaces are better for building a loosely coupled system.
 
R

rhaazy

Nicholas and Jon have already pointed out the most important reasons, butI
would like to add that interfaces allow for a more flexible design as the
same interface can be implemented by any class. If you use an abstract class
you're forced to use inheritance in order to supply instances of that type.
In other words interfaces are better for building a loosely coupled system.

--
Regards,
Brian Rasmussen [C# MVP]http://kodehoved.dk




Why would I ever use an interface when I could use an abstract class?
Assuming the only limitation is only having one base class, would this
change assuming you could implement multiple inheritance (and
guarantee not creating ambiguity issues)?
Please share your thoughts or explain something to me if I seem to be
confused.- Hide quoted text -

- Show quoted text -

Thank you everyone for your feedback, I am beginning to understand
much more of this.
 
J

Jeff Winn

The System.IDisposable interface is a very good example of why you wouldn't
need to use a base class. There are many different objects that need to be
disposable but have absolutely nothing to do with each other.

For example, compare the System.Drawing.Brush class and
System.IO.FileSystemWatcher. Both classes need to be disposed of, but are
not related.

Nicholas and Jon have already pointed out the most important reasons, but
I
would like to add that interfaces allow for a more flexible design as the
same interface can be implemented by any class. If you use an abstract
class
you're forced to use inheritance in order to supply instances of that
type.
In other words interfaces are better for building a loosely coupled
system.

--
Regards,
Brian Rasmussen [C# MVP]http://kodehoved.dk




Why would I ever use an interface when I could use an abstract class?
Assuming the only limitation is only having one base class, would this
change assuming you could implement multiple inheritance (and
guarantee not creating ambiguity issues)?
Please share your thoughts or explain something to me if I seem to be
confused.- Hide quoted text -

- Show quoted text -

Thank you everyone for your feedback, I am beginning to understand
much more of this.
 
C

Chakravarthy

By this time, if you have understood the importance between abstract class
and interface, I don't have to throw light on them.

But, thought to place some code, so that it would be meaningful for you
understand how each will behave and their respective advantages.
---------------------------------------------------------
public interface TopInterface
{
void DoSome();
}
public abstract class TopAbsClass
{
public TopAbsClass()
{
cs.WriteLine("From Private Constructor.."); // You have the
flexibility to embed your code here, which will execute every time the
inherited class initialized
}
public abstract void DoSomeWork();
}
public class FirstLevelInheritanceClass : TopAbsClass, TopInterface
{
public override void DoSomeWork() // this method is from Abstract
Class
{
Console.WriteLine("Printed from FirstLevelInheritanceClass -
DoSomeWork() Method is executed");
}
public void DoSome() // This method is from the interface
{
Console.WriteLine("Printed from FirstLevelInheritanceClass -
DoSome() Method is executed");
}
}
 
P

Pavel Minaev

Why would I ever use an interface when I could use an abstract class?

Assuming the only limitation is only having one base class, would this
change assuming you could implement multiple inheritance (and
guarantee not creating ambiguity issues)?

If you have a purely abstract class (one with all members abstract),
then there's no design difference from an interface (all limitations
of C# and .NET aside).

If you don't have a pure abstract class, and assuming that you would
have multiple inheritance with no restrictions, then it is still good
to remeber that implementation inheritance (= extending classes in C#)
is considered to be a much tighter coupling than contract inheritance
(= implementing interfaces in C#). This is described in more details
in a rather well-known "'extends' is evil" article here (written for
Java, but fully applicable to C# as well):

http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top