473,404 Members | 2,114 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,404 software developers and data experts.

Confuesd about abstract class vs interface

phl
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 required by interface also before loading
it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so that
it can be done in your inherited class. I am confused why you would
need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks

Nov 16 '05 #1
9 7534
An abstract base class is useful if you want that this class and its
descendentants cannot be instantiated unless they override *all* abstract
methods:

abstract class Foo
{
abstract void DoFoo();
}

abstract class A : Foo
{
}

class B : Foo
{
override void DoFoo();
}

You cannot create instances from A because the class did not implement to
DoFoo() method, therefore it has to be declared abstract. But you can create
instances from B since it implements DoFoo() so that is does not have to be
abstract anymore.
--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"phl" <ki************@hotmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
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 required by interface also before loading
it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so that
it can be done in your inherited class. I am confused why you would
need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks

Nov 16 '05 #2
An interface is a facade that can be placed on any class to make it conform
to a specific set of behaviours. Any class can implement any interface and
this is particularly useful in a componentised and extensible system in
which you're not sure what classes might be used by the system in the
future.

A prime example of this is the IEnumerable interface. Any class, it doesn't
even have to be a collection class, can be enumerable. The class might
synthesize some data and masquerade as a collection.

Abstract classes however are of prime importance if you need to enforce at
least which base-class is used in the derivation process.
System.Drawing.Image for example, is an abstract class and objects with the
declared type of image are used all the time. The real object will probably
be a Bitmap, which is derived from Image, but the base-class type is
enforced. The requirements of an abstract class are that the abstract
implementation MUST be provided. This enables the architect to enforce
certain rules about the class structure without having to interpret all
possible cases for implementation in advance.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"phl" <ki************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 required by interface also before loading
it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so that
it can be done in your inherited class. I am confused why you would
need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks

Nov 16 '05 #3
"cody" <no****************@gmx.net> wrote in message
news:uh**************@TK2MSFTNGP15.phx.gbl...
An abstract base class is useful if you want that this class and its
descendentants cannot be instantiated unless they override *all* abstract
methods:

abstract class Foo
{
abstract void DoFoo();
}

abstract class A : Foo
{
}

class B : Foo
{
override void DoFoo();
}

You cannot create instances from A because the class did not implement to
DoFoo() method, therefore it has to be declared abstract. But you can create instances from B since it implements DoFoo() so that is does not have to be abstract anymore.
--


Same with interfaces.
Nov 16 '05 #4
> 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 required by interface also before loading
it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so that
it can be done in your inherited class. I am confused why you would
need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks


If you create an abstract class with no method implementation and no data
members it is pretty much the same as defining an interface, no wonder why
in COM interfaces are implemented like that.

HOWEVER.

There are a number of differences.
-In abstract classes you can have data members to keep, for instance,
internal state

-In abstract classes you can provide implementation for a method:
say you have a Vehicle abstract class, you can derive Bike, MotorCycle and
Car from that, but you might want to define a method GetCC() in the base
class that will never change (no can do with interfaces)

-last but not least a C# object can implement as many interface you want but
can just inherit from one class.

Hope this helps.
Nov 16 '05 #5
phl wrote:
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 required by interface also
before loading it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so
that it can be done in your inherited class. I am confused why you
would need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks


If some class implements an interface, it declares that it (at least) has the
methods (and properties) declared in the interface. It *acts like* it
is that interface type.

If a class extends some (possibly abstract) baseclass it has some
basic functionality, plus there are some other methods that need to
be filled in. It *is a version* of that type.

Hans Kesting
Nov 16 '05 #6
An abstract class is kind of like a new car. You start out with a basic no
frills car (your abstract class), then you add too it - superduper radio,
custom paint, nice wheels. That's inheritance. You inherited the basic car
and added features via your inheriting class.

An interface is a promise that your class will implement a set of methods
specified by the interface. An instance of a class that uses another class
containing an interface knows that it can rely on that class to provide
functions promised by the interface.

"phl" <ki************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 required by interface also before loading
it.

I have never used abstract classes, as I understand it, its for
creating a base class with function without any implementation, so that
it can be done in your inherited class. I am confused why you would
need this, as you already can override functions and also theres
interfaces.

I have probably misunderstood something, can someone please explain.
The reason i am interested is because I am writing a system which i
hope to be well maintained in the future. I want other people to add
classes with specific implementations.

thanks

Nov 16 '05 #7
If you implement an interface you have to provide all methods is your class,
otherwise your code won't compile.
When using an abstract baseclass you can compile your code even when you
don't provide all abstract methods in your code - but in this case you have
to declare your class abstract and therefore cannot instantiate it.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"MaSTeR" <so***@nospam.com> schrieb im Newsbeitrag
news:32*************@individual.net...
"cody" <no****************@gmx.net> wrote in message
news:uh**************@TK2MSFTNGP15.phx.gbl...
An abstract base class is useful if you want that this class and its
descendentants cannot be instantiated unless they override *all* abstract methods:

abstract class Foo
{
abstract void DoFoo();
}

abstract class A : Foo
{
}

class B : Foo
{
override void DoFoo();
}

You cannot create instances from A because the class did not implement to DoFoo() method, therefore it has to be declared abstract. But you can

create
instances from B since it implements DoFoo() so that is does not have to

be
abstract anymore.
--


Same with interfaces.

Nov 16 '05 #8
Looking at the other responses here, I think that what's missing is an
explanation of when and why you would use an abstract class versus an
interface.

You need an interface if you want to assign common behaviour to various
classes that do not derive from the same base class (other than
Object). Bob Powell gave the excellent example of IEnumerable: you want
to be able to make many classes from all over the class hierarchy
enumerable, and in some methods treat them as "the same" for purposes
of enumeration, even though they're otherwise completely different
classes that have nothing in common.

This is different from the classic LivingThing / Animal / Mammal / Dog
/ Corgi relationship, in which the classes in the list have an "is-a"
relationship. In the world of living things, for example, you could
decide that you want some methods to be able to work with animals that
can walk. You can't have those methods accept Animal objects, because
some animals don't have legs and so cannot walk (snakes, fish, whales,
etc). So, you would need an IWalkable interface that you implement only
in certain classes (such as Dog), but where the classes that implement
it are not necessarily cleanly related in your hierarchy.

Of course, if any class implements IWalkable, this means that all of
its child classes automatically implement IWalkable by inheritance. So,
implementing IWalkable in Dog means that all types of dogs are
walkable, no matter what you do in the classes for particular breeds of
dogs.

An abstract base class is needed in a different situation. Some times
you can write most of the implementation of a class but not all of it.
Or, to look at it from the opposite point of view, perhaps you have
some classes that have a lot in common, and could really use a parent
class, but some of the methods and/or properties don't generalize well.
Again, Bob Powell gave the example of Image. There are lots of
different kinds of images: bit maps, jpegs, gifs, tiffs, etc. You want
all of those kinds of images to have (mostly) the same methods and
properties, for example you want them all to have a RotateFlip() method
to allow the caller to rotate the image, but you can't write a
meaningful implementation for that method until you know what kind of
image you're dealing with.

Without abstract base classes, you're stuck with a dilemma. If you
don't write a RotateFlip() method in the base class, then even if all
child classes declare and implement a RotateFlip() method, you won't be
able to call any of those methods if you're dealing with the Bitmap
object (for example) downcast to an Image, because Image doesn't have
that method. Or, if you implement a dummy version of the method in the
base class and someone declares a new type of Image and forgets to
override the method, then you're stuck with an Image subclass that
calls back to the dummy method that doesn't really work (because it
doesn't know how the image is represented). You could solve the problem
by adding an IRotateable interface, and insisting that all of your
child classes implement it, but now you have a base class _and_ an
interface. Yuck.

Abstract classes solve this problem nicely. They say, "There is a group
of things called Images that exhibit this behaviour. However, I can't
tell you _how_ those things _implement_ some parts of this behaviour.
That's up to my child classes."

Of course, as some people pointed out, you could create the same
external functionality by using an interface rather than an abstract
base class, but this has one enormous disadvantage: if a lot of (or
even a little of) the _implementation_ of the behaviour of an image, or
the state it needs to store, is common to all of the different kinds of
Images, then with an interface you have to repeat that code in every
subclass: you can't inherit an implementation through an interface, you
can inherit only the contract.

Nov 16 '05 #9
Interfaces do not allow definitions of members whereas abstract classes
do, through static and virtual members.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

9
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
10
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...
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
5
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
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
52
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...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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...
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...
0
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...
0
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,...
0
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...

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.