|
INTERVIEW QUESTIONS
MICROSOFT
C#
DETAILS
Question: By declaring a base class function as virtual we allow the function to be overridden in subclasses
Answer: The derived class can provide new functionality by overriding the virtual method of the base class.
The method to be called is determined at the run-time depending on the type of the object calling the method, and it does not depend on the type of the variable declared to hold the object. The following example illustrates this point:
Suppose base class B and derived class D provide different implementations of a virtual method V(). Also suppose that there is a non-virtual method NV() of B which is overriden by D.
//In Main(), an object, obj, is declared to be of type B.
B obj;
//An object of D is created for initializing obj
obj = new D;
//Call virtual and non-virtual methods on obj:
obj.V(); //This call would execute D::F() since F() is virtual, and although obj is declared to be of type B, it contains object of type D.
obj.NV(); //This call would execute B::F(), looking at the declaration of obj.
This is the difference between virtual and plain overridable methods. Virtual methods execute slower than non-virtual ones because of run-time resolve of the method to be called. So programmer should be caeful while declaring a method as virtual.
|
|
|
Category |
C# Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7797 users |
Added on |
7/28/2011 |
Views |
68845 |
Rate it! |
|
|
Question:
By declaring a base class function as virtual we allow the function to be overridden in subclasses
Answer:
The derived class can provide new functionality by overriding the virtual method of the base class.
The method to be called is determined at the run-time depending on the type of the object calling the method, and it does not depend on the type of the variable declared to hold the object. The following example illustrates this point:
Suppose base class B and derived class D provide different implementations of a virtual method V(). Also suppose that there is a non-virtual method NV() of B which is overriden by D.
//In Main(), an object, obj, is declared to be of type B.
B obj;
//An object of D is created for initializing obj
obj = new D;
//Call virtual and non-virtual methods on obj:
obj.V(); //This call would execute D::F() since F() is virtual, and although obj is declared to be of type B, it contains object of type D.
obj.NV(); //This call would execute B::F(), looking at the declaration of obj.
This is the difference between virtual and plain overridable methods. Virtual methods execute slower than non-virtual ones because of run-time resolve of the method to be called. So programmer should be caeful while declaring a method as virtual. Source: CoolInterview.com
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.
|
|
Related Questions |
View Answer |
|
Which of the following is not a C# reserved keyword
|
View Answer
|
|
The compiler throws an error if XML comments is not well formed
|
View Answer
|
|
How do you choose 1 entry point when C# project has more Main( ) method?
|
View Answer
|
|
Is it possible to inherit a class that has only private constructor?
|
View Answer
|
|
How can i check whether a dataset is empty or not in C#.net
|
View Answer
|
|
What is XML Schema?
|
View Answer
|
|
What is the default Function arguments?
|
View Answer
|
|
How do i read the information from web.config file?
|
View Answer
|
|
What is object pooling
|
View Answer
|
|
What is the main difference between pointer and delegate with examples?
|
View Answer
|
|
How to convert ocx into DLL
|
View Answer
|
|
This is a Regular expression built for parsing string in vb.net and passed to Regex class. Dim r As Regex = New Regex(",(?=([^""]*""[^""]*"")*(?![^""]*""))")
What is C# equivalent for this regular expression.
|
View Answer
|
|
Why strings are immutable?
|
View Answer
|
|
Why multiple Inheritance is not possible in C#?
|
View Answer
|
|
Constructor is the method which is implicitly created when ever a class is instantiated. Why?
|
View Answer
|
|
What Datatypes does the RangeValidator Control support?
|
View Answer
|
|
Where we can use DLL made in C#.Net
|
View Answer
|
|
How can objects be late bound in .NET?
|
View Answer
|
|
How to find exceptions in database
|
View Answer
|
|
How we can use inheritance and polymorphisms in c# programming?
|
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
|