CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: What does the keyword virtual mean in the method definition?

Answer: If a base class have a virtual method it means that method must be override by the derived class using override keyword.



Category C# Interview Questions & Answers - Exam Mode / Learning Mode
Rating (4.2) By 58 users
Added on 7/28/2011
Views 2593
Rate it!
Question: What does the keyword virtual mean in the method definition?


Answer:

If a base class have a virtual method it means that method must be override by the derived class using override keyword.
Source: CoolInterview.com


A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes
or its called do nothing function . Source: CoolInterview.com

Answered by: Ajay Verma | Date: 1/17/2008 | Contact Ajay Verma Contact Ajay Verma

virtual keyword is used in the base class method
when derived class use virtual method we use override key with derived class method and the implementation of method can be changed but not affecting the type signature of base class.

eg: base class
public virtual method1()
{
int a,b,c;
c=a+b;
Console.WriteLine(c);
}

derived class

public override method1()
{
int a,b,c;
c=a*b;
Console.WriteLine(c);
}


Source: CoolInterview.com

Answered by: ranjeeth | Date: 2/26/2008 | Contact ranjeeth Contact ranjeeth

virtual meathod is define in base class and which is over-ridden in child class..
base:class
public virtual method1()
{
string c="virtual method1"
Console.WriteLine(c);
}

derived class

public override method1()
{
string c="override meathod"
Console.WriteLine(c);
} Source: CoolInterview.com

Answered by: rahil mohd | Date: 4/5/2009 | Contact rahil mohd Contact rahil mohd

If a Method is declared as Virutval in Base class then you can override the Method in derived Class by using Override keyword.

class Program
{
static void Main(string[] args)
{
CA a;
a= new CB();
a.Foo();
}

}
class CA
{
public virtual void Foo()
{
Console.WriteLine("Foo in parent Class");
}
}
class CB : CA
{

public override void Foo()
{
Console.WriteLine("Foo in Child Class");
}
} Source: CoolInterview.com

Answered by: Nageswara Rao | Date: 8/13/2009 | Contact Nageswara Rao Contact Nageswara Rao

Basically virtual method has drastical affect on performance of application. No Doubt we can do all the things without virtual function but if we use Virtual then we have following benefits.

1. Same of name of function can be used in derived class after overriding and can change its body there but with same signature

2. Overrided function will point to same memory location that is of the virtual function. means same memory location can be shared.
Source: CoolInterview.com

Answered by: Gurpreet Singh | Date: 11/27/2009 | Contact Gurpreet Singh Contact Gurpreet Singh

A method declared as virtual in the base class can be implemented in the derived class with override keyword. But it is not a must. If we use new keyword it hides the method Source: CoolInterview.com

Answered by: Titty Louis | Date: 8/14/2010 | Contact Titty Louis Contact Titty Louis


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
How's method overriding different from overloading?

View Answer
What's the top .NET class that everything is derived from?

View Answer
Describe the accessibility modifier protected internal.

View Answer
Are private class-level variables inherited?

View Answer
When you inherit a protected class-level variable? Who is it available to?

View Answer
Does C# support multiple inheritance?

View Answer
How do you inherit from a class in C#?

View Answer
What's the implicit name of the parameter that gets passed into the class' set method?

View Answer
Is it mandatory to implement all the methods which are there in abstract class if we inherit that abstract class..?

View Answer


What is the difference between const and static read-only?



View Answer
How to fill datalist usinf XML file and how to bind it,code behind language is c#

View Answer
WHAT IS THE ADVANTAGE OF SERIALIZATION?

View Answer
How we hand sql exceptions? What is the class that handles SqlServer exceptions?

View Answer
What is indexer? where it is used plz explain

View Answer
can we inherit the java class in C# class,how?

View Answer
We declare an integer variable 'x' in C# as

View Answer
A code block in C# is enclosed between

View Answer
To read user input from the console, the following statement is used

View Answer
The following is a correct call to the Main() function

View Answer
All the C# programs must have the following statement

View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All C# Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
"Holiday Packages" announced as an advertisement are an example of _________: (a) Offer (b) Counter Offer (c) Invitation to Offer (d) None of the above
A match fixing contract between a player and a broker is a: (a) Valid Contract (b) Unenforceable Contract (c) Void Contract (d) Illegal Contract
When a bookseller sells a book on cash payment then it is called as ___________: (a) Unilateral Contract (b) Bilateral Contract (c) Executed Contract (d) Executory Contract
____________ agreements are created by situation: (a) Written (b) Oral (c) Void (d) Implied
Cache = 0.03125 Seconds