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