ShowMsg() {
System.Console.WriteLine("From Child meth");
}
}
class Class1 {
[STAThread]
static void Main(string[] args) {
Parent m_pParent=
new Parent(); m_pParent.ShowMsg();
m_pParent=
new Child(); //assign child instance to parent ref var m_pParent.ShowMsg();
//would call parent's meth if its not for the virtual keyword
}
}
On the other hand an abstract method in an abstract class would ALWAYS BE OVERRIDEN in the inheriting class because the base class could not find any implementaton for the abstract method. the abstract keyword both for the class and the method
for ex:
public abstract class Parent
{
public abstract void ShowMsg(); //NO IMPLEMENTATION DEFINED!
}
the inheriting class needs to imlpement the ShowMsg method. Here too the child class instance can be referenced by the abstract class pointer(virtual keyword is not required now).
the most important deferences between virtual methods and abstract methods are:
1) virtual methods have implementation whereas abstract methods have no implementation in the base class.
2) abstract methods are ALWAYS declared in an abstract class.
3) abstract classes cannot be instanciated on their own. Only child classes that have provided an implementation for the abstract method can be instanciated.
4) a child class has to implement ALL the abstract methods so as to qualify for creating an instance of the child class. whereas a child class can choose to ovveride only those vitual methods which it thinks is invalid for the present context. all the virtual methods that have not been ovveriden in the child class behave in the way defined in the base class.
I think that is all!
- Vamshi Krishna ([EMAIL PROTECTED])