473,834 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface vs. Abstract Class

I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to using
one or the other?

Brad
Nov 17 '05 #1
18 3789
Bradley wrote:
I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to using
one or the other?


Seems a good place to start is by looking at the difference between the
two. For starters someone implementing an interface can implement
multiple interfaces and can also have a base class. With an abstract
base, that is the only base class they can have, although they could
implement other interfaces. What this means is that two classes that
are completely unrelated in any other way could both implement your
interface. With an abstract class, anyone inheriting from your abstract
class would be more closely related in that they would all have a same
base type (your abstract class) in their inheritance hierarchy.

With an abstract class you can, if you want or need, provide some
implementation in the abstract class. An interface can provide no
implementation.
--
Tom Porterfield
Nov 17 '05 #2
Well, if the implementations share a lot of code, you're probably looking at
an abstract class. On the other hand, if you just want the programming
interface to conform to a certain set of sigunatures, and there is little or
no possibility that the implementations will share code, you're probably
looking at an Interface.

That's it in a nutshell. Broadly speaking.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Bradley" <br************ @roche.com> wrote in message
news:OD******** ******@TK2MSFTN GP14.phx.gbl...
I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to using
one or the other?

Brad

Nov 17 '05 #3
Thanks for the responses. It sounds like unless you need to inherit
implementation code, there's very little difference.

Brad

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:eK******** ******@TK2MSFTN GP15.phx.gbl...
Well, if the implementations share a lot of code, you're probably looking at an abstract class. On the other hand, if you just want the programming
interface to conform to a certain set of sigunatures, and there is little or no possibility that the implementations will share code, you're probably
looking at an Interface.

That's it in a nutshell. Broadly speaking.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"Bradley" <br************ @roche.com> wrote in message
news:OD******** ******@TK2MSFTN GP14.phx.gbl...
I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to using one or the other?

Brad


Nov 17 '05 #4
Ditto. But when I do it, I tend to create both (i.e., the abstract/base
class implements an interface). I've found this useful to have when all of
a sudden I find the abstract class doesn't cut it anymore, and have to go to
the interface. I figured since it's done a lot in the framework, I may as
well do it too!!

Derrick
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:eK******** ******@TK2MSFTN GP15.phx.gbl...
Well, if the implementations share a lot of code, you're probably looking
at an abstract class. On the other hand, if you just want the programming
interface to conform to a certain set of sigunatures, and there is little
or no possibility that the implementations will share code, you're
probably looking at an Interface.

That's it in a nutshell. Broadly speaking.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"Bradley" <br************ @roche.com> wrote in message
news:OD******** ******@TK2MSFTN GP14.phx.gbl...
I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to
using
one or the other?

Brad


Nov 17 '05 #5
Brad,

This is how I would start: Initially if I am not sure which one to
use, then I would use an Interface. Now, once you have a few
implementation classes for that interface, you may realize that there
are some methods that have code that are always the same between the
classes. You can extract out those methods; one way is to extract them
out to a common parent class. In this case, the new implementation
class would need to inherit from the parent class and implement the
interface. If the parent class and the interface are always used
together, then you may want to use an abstract class instead to group
them together, so you won't be accidentally using just the interface
but not the parent class.

HTH,
Alex

Nov 17 '05 #6
Bradley... After you code for a while you will discover the sudden need
to add a method to an interface. This is a bad place to be. Changing the
interface will break all clients. Soooooo. You need to refactor the
interface so that you can easily extend the framework. You need to be
sure about the interface. If you are not sure about the interface you
may want to start with an abstract class. If you add a method to the
abstract class, you can add a default implementation and not break any
clients.

Regards,
Jeff
Is there any design advantage to using
one or the other?
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7

"Bradley" <br************ @roche.com> wrote in message
news:OD******** ******@TK2MSFTN GP14.phx.gbl...
I'm trying to determine if there's a general rule for when an Interface
should used vs. an Abstract Class. Is there any design advantage to using
one or the other?

Brad


Interfaces are most appropriate when defining contracts between types that
are invariant over time.
You also need to use interfaces when you want to provide polymorphic value
type hierarchies (value types cannot inherit from other types).
But in general you would prefer abstract types over interfaces as they
version much better and they are extensible.

Willy.
Nov 17 '05 #8
Jeff Louie <je********@yah oo.com> wrote:
Bradley... After you code for a while you will discover the sudden need
to add a method to an interface. This is a bad place to be. Changing the
interface will break all clients. Soooooo. You need to refactor the
interface so that you can easily extend the framework. You need to be
sure about the interface. If you are not sure about the interface you
may want to start with an abstract class. If you add a method to the
abstract class, you can add a default implementation and not break any
clients.


On the other hand: use mock frameworks for unit testing is *much*
easier with interfaces.

I do take your point about breaking changes, but it's worth noting that
lots of people are in lots of different deployment situations. In many
cases, you know all the implementors of an interface, and can change
them all appropriately. In many other cases, you really can't do that -
so you have to end up creating another interface, etc. (Icky.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Jon... In fact, I relentlessly code to an interface in this situation.
If I need to add a method to the interface the compiler immediately
finds all of the invalid classes for me!!!!! Then I just update the
classes until the compiler stops complaining :).

Regards,
Jeff
In many cases, you know all the implementors of an interface, and can
change them all appropriately.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #10

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

Similar topics

9
4658
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
20
4267
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
9
7562
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you can also check for interface at run time say when dll is loaded and see if it implememts whats...
10
2988
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
10
667
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what circumstacnes does one implement an interface? TIA, -- Joe VBA Automation/VB/C++/Web and DB development
5
1844
by: Tony Johansson | last post by:
Hello!! Assume you have an Interface called ITest with these three method declarations. interface ITest { void foo1(); void foo2(); void foo3(); }
7
1658
by: gordon | last post by:
Hi I am working through some course notes for a msdn training course in C# and I am a little stuck with the differences between an abstract class and an interface. Could someone please give me a short understanding of what the differences are? I understand that the abstract class, if called must have its methods and
52
20923
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
7
255
by: tshad | last post by:
I am trying to understand Interfaces and why I would use them. I have an example from a book that is explaining it. I just can't seem to see why I would use it. In my example, it has 2 interfaces: IDisplayable and IComparable (Predefined). In the Students class part, he is moving the students array to an
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7755
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.