473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Interfaces vs Classes

I am trying to figure out when it is best to use Interfaces and when to use
classes.

The only reason I can figure out to use interface is if you need to
implement multiple interfaces.

The problem with interfaces over classes seem to be that you need to create
multiple implementations for the same interface using interfaces (which may
be the same for all your classes).

Why would you not just implement an class where you can extend the class and
just override the classes you want to be different?

I was looking at a program from a book that set up interfaces for its
database class but then just implements the Database class itself. Not sure
why the interface was necessary.

I see a lot of discussions on Interfaces, but not a lot of discussions on
why you would use one over the other (other than the multiple interface
issue). It's one thing to know how to do it, quite another to know when to
use it (just because you can, doesn't mean you should)

Am trying to find the pros and cons of each method.

Thanks,

Tom
Nov 19 '05 #1
7 1136
A good example is when you create a Windows Service.
Every windows service must be able to start, stop, pause, resume, etc. And
yet those commands can mean very different things depending on the nature of
the windows service.
Therefore an interface is perfect (and is required for windows services)
because each service must implement the same set of methods and yet they are
implemented in a wide variety of ways.

Here's more info on Windows Services:
http://msdn.microsoft.com/library/de...plications.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <tf*@dslextreme.com> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
I am trying to figure out when it is best to use Interfaces and when to use
classes.

The only reason I can figure out to use interface is if you need to
implement multiple interfaces.

The problem with interfaces over classes seem to be that you need to
create
multiple implementations for the same interface using interfaces (which
may
be the same for all your classes).

Why would you not just implement an class where you can extend the class
and
just override the classes you want to be different?

I was looking at a program from a book that set up interfaces for its
database class but then just implements the Database class itself. Not
sure
why the interface was necessary.

I see a lot of discussions on Interfaces, but not a lot of discussions on
why you would use one over the other (other than the multiple interface
issue). It's one thing to know how to do it, quite another to know when
to
use it (just because you can, doesn't mean you should)

Am trying to find the pros and cons of each method.

Thanks,

Tom

Nov 19 '05 #2
Interface defines a contract , while classes are implementation.

C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/

Nov 19 '05 #3
Interfaces and abstract base classes are _almost_ the same thing.
Almost, but not quite.

Let me give you a real life example.

In our current project we work with a kind of plug in system. It's all
about different developers creating pieces of functionality for one big
application.

In order to make sure the core application understands the plugins, the
developers need to implement the IPlugin interface. That way we know
that we can alway rely on having all the methods we need to instantiate
the plugin (ie. IPlugin.Instantiate, IPlugin.Draw() etc.. )

We don't care what the base classes of the plugins are. That's up to
the individual developer. We just want to use their IPlugin
capabilities.

Nov 19 '05 #4
You are right about multiple inheritence with interfaces. That is the
only way to do it.

When to use one over the other depends on your situation. As shiv
mentioned, an interface is a contract. this means that everyone _must_
implement every method that is in the interface. And as Steve said, it
is a good way for different classes to behave differently, but still be
related.

One big draw back (for me at least) to interfaces is they are very
brittle when it comes to version changes. Say you have an interface
with 4 methods / properties and have several classes implementing that
interface. If you add or remove a method or property from the
interface, now all classes implementing it are no longer meeting their
interface contract obligations and they will not build. In some cases,
this could be a good thing.

However, if you add a new method/property to an inherited class. The
classes that inherited won't mind if there is a new method in the base
class. They will just use base implementation of the method and go on
about their business.

Nov 19 '05 #5

"john_teague" <jc******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
[snip]
One big draw back (for me at least) to interfaces is they are very
brittle when it comes to version changes. Say you have an interface


[snip]

Good point, I've often wondered how Interface changes should be handled with
version changes.

You can argue that an interface that needs changing was probably not
properly designed in the first place; but the real world indicates that it
is not only a possible change version to version but changing at least one
interface is probable.

What do you do, add the version number to the end?
IWidget
IWidget2
Or
IWidgetX

IMHO, that's an ugly hack!

Or do you just break every extension?

I suppose this is really only a problem for Frameworks (including apps which
support add-ins & customization), but I'd like to know how should this be
handled?

Looking forward to your input.

--
Regards,
John MacIntyre
http://www.johnmacintyre.ca
Specializing in; Database, Web-Applications, and Windows Software
Nov 19 '05 #6
"Dennis Vroegop" <dv******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Interfaces and abstract base classes are _almost_ the same thing.
Almost, but not quite.

Let me give you a real life example.

In our current project we work with a kind of plug in system. It's all
about different developers creating pieces of functionality for one big
application.

In order to make sure the core application understands the plugins, the
developers need to implement the IPlugin interface. That way we know
that we can alway rely on having all the methods we need to instantiate
the plugin (ie. IPlugin.Instantiate, IPlugin.Draw() etc.. )

We don't care what the base classes of the plugins are. That's up to
the individual developer. We just want to use their IPlugin
capabilities.
But couldn't you do the same with Base Classes?

Set up all the Methods in the Base Class (even if the Method does nothing)
and whatever the inherited classes don't set up will use the base methods.

Tom

Nov 19 '05 #7
Yes, I really think that Interfaces make the most sense in large-scale
frameworks. All of the work I do, which is almost completely
enterprise apps, I do not use Interfaces frequently. I tend toward
using base classes. The reason being, requirements have a tendency to
change more frequently and interfaces create more maintenance for me.
I would not use a renaming approach to new versions. I would instead
strongly name your dll and put breaking changes in a new assembly with
a different version number. That way the end user can choose which
version and which interface to implement.

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
7
by: Ant | last post by:
Hi, I’m wondering what practical use is there for creating interfaces. I can fully appreciate creating an abstract class for the purpose of inheriting, but why an interface? Is it just to...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
6
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
8
by: Dave | last post by:
I have a set of developers who have gone off and implemented an interface for nearly all classes in a project\solution, now some of these classes will need interfaces as they implement the provider...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
18
by: abcd | last post by:
Can someone tell me whats the exact advantage of Interfaces in C#. To me abstract classes serves the purpose and have all good things then what is special about interfaces...can someone tell me...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.