By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,668 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,668 IT Pros & Developers. It's quick & easy.

Object Oriented Specific Question

P: n/a
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.
Jul 9 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rhaazy" <rh****@gmail.comwrote in message
news:dd**********************************@l64g2000 hse.googlegroups.com...
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.

Jul 9 '08 #2

P: n/a
rhaazy <rh****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 9 '08 #3

P: n/a
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");
}
}
---------------------------------------------------------

It is recommended to use interfaces, only due to "no code but template".
Hope this helps you


"rhaazy" <rh****@gmail.comwrote in message
news:dd**********************************@l64g2000 hse.googlegroups.com...
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.
Jul 10 '08 #4

P: n/a
On Jul 9, 9:46*pm, rhaazy <rha...@gmail.comwrote:
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/j...1-toolbox.html
Jul 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.