Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shadows 1

Status
Not open for further replies.

transparent

Programmer
Sep 15, 2001
333
GB
I am currently reading the MCAD Training manual for web apps.

The book talks about derived classes containing methods that shadow a base class method.

I'm not sure why you would want to do this. Why not just override?
 
Let's say you need a class which will hold a cost (of a call, for example). Then you will need a class which manipulates the call (i.e. by adding a tax.)

public class RawCall
{
public decimal m_dCost;

public RawCall( decimal dmInitialCost)
{
m_dCost = dmInitialCost;
}

public virtual decimal GetCost()
{
return m_dCost;
}
}

public class ProcessedCall : RawCall
{
public int m_iTaxPercent;
public new decimal m_dCost;

public ProcessedCall( decimal dmCallCost) : base( dmCallCost)
{
m_dCost = dmCallCost;
}

public void ApplyTax()
{
m_dCost = m_dCost * ( 100 + m_iTaxPercent) / 100;
}

public new decimal GetCost()
{
return m_dCost;
}
}

Now, let's assume you need to display the cost of the call without tax and with tax in the same report/form/etc..

You can simply do that with the following code:

ProcessedCall aProcCall = new ProcessedCall( 10);
RawCall aRawCall = (RawCall)aProcCall;

aProcCall.m_iTaxPercent = 20;
aProcCall.ApplyTax();

MessageBox.Show( aProcCall.GetCost().ToString()); //Will show 12
MessageBox.Show( aRawCall.GetCost().ToString() );//Will show 10
 
But wouldnt this do the same if you just used overloading?
 
Placing override instead of new in the declaration of the GetCost method from the ProcessedCall class will determine the compiler to look up in the vtable the GetCost method and the result will be the ProcessedCall::GetCost.

Shadowing is hiding the members.
Overloading means that the vtable will contain a pointer to the overloaded member. That means that even if you have a pointer/reference to the base class, the method from the derived class will be executed if the actual object is of derived class type.
This whole thing (excuse my english, I hope I made myself clear) is called polymorphism [bigglasses].
 
I think I get it...

To re-iterate...

ProcessedCall aProcCall = new ProcessedCall( 10);
RawCall aRawCall = (RawCall)aProcCall;

If overloading is used, in both cases the derived method will be executed and so:

MessageBox.Show( aProcCall.GetCost().ToString());
MessageBox.Show( aRawCall.GetCost().ToString() );

will return the same result?

Using the new keyword, means that when you call aRawCall.GetCost() the base class method is called because the 'new' method is hidden?


 
Yes, both methods will return the same result.
"New" keyword hides the method in the base class.

Using the new keyword, means that when you call aRawCall.GetCost() the base class method is called because you "touch" the object with a reference to the base class and the compiler realizes you want access to the hidden field (this is the only method of accessing the hiddend field/method/property/etc, as far as I know)

Check the "new modifier" section here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top