472,958 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Object Oriented Specific Question

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
4 1021
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ricardo Batista | last post by:
I need that someone help me. I need to program the dijkstra algorithm by object oriented. I'll send my code. #!/usr/bin/env python # -*- encoding: latin -*- NIL =
6
by: Jessica | last post by:
Hi, I have a question regarding the object-oriented issue. I understand that a class is a type. I have an array class. Now say that I want to implement an algorithm A that uses the array class....
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
1
by: Patrick | last post by:
Hi all, New to .NET, I'm working on an Winforms client application using VS 2005 beta2. My needs considering data storage are the followings: (1) Small files (0 < length < 10 mb), containing...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
by: topmind | last post by:
siddharthkh...@hotmail.com wrote: Good luck. The behavior and conventions of web versus fat-client (or paper-oriented) reports are so different that making a generic anything that serves both of...
7
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.