469,621 Members | 2,244 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

diff between abstract class and interface

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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug
Sep 14 '06 #1
7 1526
gordon wrote:
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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug

I am sure you can google a lot of answers to this question. But one
quick notes is that:

you can ONLY inherit from one abstract class, but you can implement a
lot of interfaces.
Sep 14 '06 #2
"gordon" <go**********@optusnet.com.auwrote in message
news:45***********************@news.optusnet.com.a u...
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
members included in the derived class - but it appears that is the same
for the interface.

Any links to information about the differences would be useful to.
An abstract class can contain code, an interface cannot.
A class can implement many interfaces but can only be inherited from one
class.
An abstract class can have protected members.

Michael
Sep 14 '06 #3
I would add, correct me if I'm wrong, that an abscract class could have
static methods while the interface cannot...

gordon a écrit :
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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug
Sep 14 '06 #4
Hi Gordon,

gordon napisa³(a):
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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug
There is a lot of knowledge on this newsgroup that you can
acquire.

In short:

An *interface* is a contract, that should be provided in the
class (or structure) that implements it.

An *abstract_class* is a class that can not be instantiated
directly, because it contains the *abstract* members (methods
or properties).

The usually, way of class design look like this:

1. define an interface, e.g.:
public interface IMyInterface
{
}

2. implement an abstract class, e.g.:
public abstract class MyClassBase: IMyInterface
{
}

3. implement a class, e.g.:
public class MyClass: MyClassBase
{
}

The power of abstract class is:

1. You can force its constuctor parameters, e.g.:
protected MyClassBase(int myFirstParam, string mySecondParam) {
}

2. You can provide a way of code execution, e.g.

// in MyClassBase
protected abstract int Execute(string firstParam);

public int Execute() {
return this.Execute("My string");
}

// so you'll have to provice in MyClass
protected override int Execute(string firstParam) {
return "My result";
}

with regards
Marcin
Sep 14 '06 #5
Read there http://www.codeproject.com/csharp/ab...interfaces.asp

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"gordon" wrote:
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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug
Sep 14 '06 #6
Actually in IL you can have static methods on either .. no high level
language that I know of supports this but the CLR does (static methods are
simply bound to a type)

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ba******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
I would add, correct me if I'm wrong, that an abscract class could have
static methods while the interface cannot...

gordon a écrit :
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
members included in the derived class - but it appears that is the same
for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug

Sep 14 '06 #7
Following are the significant differences between an abstract class and an
interface:

1. In an abstract class some of the methods may be defined and some of them
may be abstract. In case of an Interface, none of the methods can be defined.

2. In an interface all the members must be public. In an abstract class this
is not the limitation.

3. A class can inherit from one abstract class but a class can implement
many interfaces.

--
Nand Kishore Gupta
"gordon" wrote:
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
members included in the derived class - but it appears that is the same for
the interface.

Any links to information about the differences would be useful to.

Thanks in advance

doug
Sep 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Joe | last post: by
9 posts views Thread by Sean Kirkpatrick | last post: by
3 posts views Thread by chandu | last post: by
7 posts views Thread by jason | last post: by
4 posts views Thread by David Zha0 | last post: by
52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
6 posts views Thread by Miguel Guedes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.