CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS MICROSOFT C# DETAILS
Question :
What is the difference between shadow and override


Category C# Interview Questions
Rating (4.9) By 28 users
Added on 8/12/2006
Views 3154
Rate it!
Answers:

Shadowing :- This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword "Shadows". The method signature,access level and return type of the shadowed member can be completely different than the base class member.

Hiding : - This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword "new". The method signature,access level and return type of the hidden member has to be same as the base class member.Comparing the three :-

1) The access level , signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands the these parameters as same.

2) The difference lies when you call the derived class object with a base class variable.In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.



There are two main Differences between Shadowing and Overriding.
1) Overriding redefines only the implementation but shadowing redefines the whole Element.
2)In Overriding dervied Class can refer the Base class using ME Keyword but in shadowing we can access it using "MYBase".



 Posted by: Praveen Sundar P.V    

Contact Praveen Sundar P.V  Contact Praveen Sundar P.V

Shadowing: It hides a base class member in the derived by using the "New" keyword.
It is used to explicitly hide a member inherited from base class.
new and override both cannot be used for the same member.

eg:-
class Employee
{
public virtual double CalculateSalary()
{
return basic_salary;
}
}
class SalesPerson: Employee
{
double sales,comm;
public new double CalculateSalary()
{
return basic_salary+(sales*comm);
}
}
static void main(string[] args)
{
SalesPerson sal=new SalesPerson();
double s=sal.CalculateSalary();
Console.WriteLine(s);
}

Overriding:In this methods have same names,same signatures,same return types but in different classes.
C# uses virtual and override keyword for method overriding.

eg:-
class Employee
{
public virtual double CalculateSalary()
{
return basic_salary;
}
}
class SalesPerson: Employee
{
double sales,comm;
public override double CalculateSalary()
{
return basic_salary+(sales*comm);
}
}
static void main(string[] args)
{
Employee sal=new SalesPerson();
double s=sal.CalculateSalary();
Console.WriteLine(s);
}

When you call the derived class object with a base class variable.In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.



 Posted by: Anant    

Contact Anant  Contact Anant


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is the top .NET class that everything is derived from?

View Answer
C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?

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 is an abstract class?

View Answer
What does the keyword virtual mean in the method definition?

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

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C# Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/10649/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.91