diff between abstract class and interface

G

gordon

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
 
J

Jianwei Sun

gordon said:
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.
 
M

Michael C

gordon said:
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
 
B

baramuse

I would add, correct me if I'm wrong, that an abscract class could have
static methods while the interface cannot...

gordon a écrit :
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

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
 
G

Greg Young

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

I would add, correct me if I'm wrong, that an abscract class could have
static methods while the interface cannot...

gordon a écrit :
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top