New Message on dotNET User Group Hyd

Question Of The Day !!!! -- (14-06-2005)

Reply
  Reply to Sender   Recommend Message 4 in Discussion
From: vamsum_kris

A class uses the virtual methods if it expects to be a base class and the virtual method may be overridden by the inheriting class. The inheriting class may choose to override the virtual method to provide its own implementation.
virtual methods is the essence of polymorphism. it allows the new implementation of the method(in the inherited class) to be used even if the child class is referred by a base class pointer.
To make it clear consider a base class:
 

class Parent

{

public virtual void ShowMsg()

{

System.Console.WriteLine("From Parent meth");

}

}

class Child:Parent

{

public override void 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])

 


View other groups in this category.

Click Here
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to