473,805 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference Between Interface and Abstract Class

Difference Between Interface and Abstract Class?

Oct 2 '07 #1
9 3043
Hi,

That is the same as an image of a chick and an egg (for vb a scrambled egg).

(An Interface shows the contract how a class should look, an abstract class
(the name in VB is mustinherit) is a real class, however you have always to
inherit it).

Cor
Oct 2 '07 #2
"msbs1984" <u37888@uweschr ieb
Difference Between Interface and Abstract Class?
In addition to Cor,

If there is no common code and no common fields (=class level variables) of
all classes that would inherit from an abstract class, declare an interface
instead.
Armin

Oct 2 '07 #3
On Oct 2, 7:18 am, "Armin Zingler" <az.nos...@free net.dewrote:
"msbs1984" <u37888@uweschr ieb
Difference Between Interface and Abstract Class?

In addition to Cor,

If there is no common code and no common fields (=class level variables) of
all classes that would inherit from an abstract class, declare an interface
instead.

Armin
In addition to Cor and Armin,

Remember that a class can only inherit a single class, but can
implement any number of interfaces. For that reason I normally try to
use interfaces unless there is absolutely a terrific reason to use an
abstract class. It allows me more flexibility if a project is re-
purposed (of course we know no managers would ever change a project's
design mid way through...)

By the Cor, I like the "(for vb a scrambled egg)" line. :-)

Thanks,

Seth Rowe

Oct 2 '07 #4
"rowe_newsgroup s" <ro********@yah oo.comschrieb
Remember that a class can only inherit a single class, but can
implement any number of interfaces.
Good point. :)
Armin
Oct 2 '07 #5
"msbs1984" <u37888@uweschr ieb:
Difference Between Interface and Abstract Class?

'MustInherit' classes can contain implementation code whereas interfaces are
just interfaces.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 2 '07 #6
On Oct 2, 6:37 am, "msbs1984" <u37888@uwewrot e:
Difference Between Interface and Abstract Class?
They're used for different purposes and different reasons:

Interface usage example:

I have a dll with, say, 10 different classes, each one does something
totally different than the rest. (Actual app was communication
decoding, needed to be able to decode several different communications
protocols, created a separate class for each one.)

I wouldn't know until run time which of these classes I had to use.
(The app called a dll which read a config file which told the dll
which protocol class to load.)

Since I didn't know until run time which class to use, I couldn't
declare a specific class instance. (I could have had some branching
code in the app but then I'd have to recompile and reinstall every
time I wanted to add a new protocol in the future. More on this
later.)

I declared an interface and the interface was the "face" that all
these classes presented to the app.

I did not want the app to be aware of or have to deal with these
different classes, so I just declared an object variable as though it
were going to be an instance of the interface just like it was a
class.

Elsewhere, I created an instance of the appropriate class and passed a
referrence to the interface object variable.

Meanwhile, the app dealt with just the interface, never knowing what
was behind the interface. Regardless of what actual class instance
was "plugged into" the other side of the interface, the same face was
always presented to the app.

Abstract Class Example:
In the above app, the 10 decoding classes actually shared quite a bit
of common logic. So first I created an abstract class that
implemented the interface and also implemented all the common logic.
Then I created the 10 classes, each one inheriting the base class
already containing all the common logic and interface, and then
finally implemented unique decoding logic in each of the classes.

Another interface example:
In later generations of this app, I wanted to be able to upgrade and
add decoding modules to an installed copy of the app later without
having to recompile and reinstall the entire app every time I wanted
to add a new protocol. (Essentially I wanted to be able to add more
communications drivers in the future just by copying in a new dll with
the new protocol.)

So again, the main app just dealt with the interface and never knew
where the actual class instance that did the decoding came from. A
separate dll read a config file, some info in the config file told the
dll whether the protocol to use would be found in one of the compiled
classes or in a new driver dll that had been copied in, the name of
the new dll, and the name of the class in the dll to use. The dll
would open the new driver dll, create an instance of the desired
decoding class and pass it back to the interface object variable owned
by the app. The app never knew nor cared where or how the actual
class instance came from. All it had to deal with was the same
interface regardless of where or how the actual class instance got
there.

Oct 4 '07 #7
On Oct 2, 5:37 am, "msbs1984" <u37888@uwewrot e:
Difference Between Interface and Abstract Class?
Just another less-than-obvious difference is that adding a member to
an interface breaks version compatibility whereas adding a member to
an abstract class does not.

Brian

Oct 4 '07 #8
Brian,
>Just another less-than-obvious difference is that adding a member to
an interface breaks version compatibility whereas adding a member to
an abstract class does not.
If you add an abstract method to a class it will break existing
derived classes.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 4 '07 #9
On Oct 4, 4:53 pm, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
wrote:
Brian,
Just another less-than-obvious difference is that adding a member to
an interface breaks version compatibility whereas adding a member to
an abstract class does not.

If you add an abstract method to a class it will break existing
derived classes.

Mattias

--
True. Good clarification.

Oct 5 '07 #10

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

Similar topics

1
4253
by: G. Smith Q news | last post by:
What is the difference between an abstract class and an Interface. As a novice, they both seem to serve the same purpose?
9
4655
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
4266
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
10
2984
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
1843
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(); }
4
3927
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
8
20614
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the fundanmental concept so i can remember it forever as it is a common interview question.... Thanks in advance
52
20916
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...
0
9596
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
10613
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
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...
1
7649
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
6876
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
5544
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
4327
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
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.