Polymorphism question

T

tshad

If I have the following classes where I am overloading (overriding) a method
from the base classes (in this case - Withdraw), I can get to the inherited
classes method by using base.method.
******************************************
public class BankAccount
{
// Withdrawal - you can withdraw any amount up to the
// balance; return the amount withdrawn
virtual public void Withdraw(double dWithdraw)
{
Console.WriteLine(" calls BankAccount.Withdraw()");
}
}

// SavingsAccount - a bank account that draws interest
public class SavingsAccount : BankAccount
{
override public void Withdraw(double dWithdrawal)
{
base.Withdraw(200);
Console.WriteLine(" calls SavingsAccount.Withdraw()");
}
}

// SpecialSaleAccount - account used only during a sale
public class SpecialSaleAccount : SavingsAccount
{
override public void Withdraw(double dWithdrawal)
{
base.Withdraw(200);
Console.WriteLine(" calls SpecialSaleAccount.Withdraw()");
}
}
*********************************************************
But in the SpecialSaleAccount - can I get to the base class of the inherited
class (BankAccount) directly?

Thanks,

Tom
 
J

Jeroen Mostert

tshad said:
If I have the following classes where I am overloading (overriding) a method
from the base classes (in this case - Withdraw), I can get to the inherited
classes method by using base.method.
******************************************
public class BankAccount
{
// Withdrawal - you can withdraw any amount up to the
// balance; return the amount withdrawn
virtual public void Withdraw(double dWithdraw)
{
Console.WriteLine(" calls BankAccount.Withdraw()");
}
}

// SavingsAccount - a bank account that draws interest
public class SavingsAccount : BankAccount
{
override public void Withdraw(double dWithdrawal)
{
base.Withdraw(200);
Console.WriteLine(" calls SavingsAccount.Withdraw()");
}
}

// SpecialSaleAccount - account used only during a sale
public class SpecialSaleAccount : SavingsAccount
{
override public void Withdraw(double dWithdrawal)
{
base.Withdraw(200);
Console.WriteLine(" calls SpecialSaleAccount.Withdraw()");
}
}
*********************************************************
But in the SpecialSaleAccount - can I get to the base class of the inherited
class (BankAccount) directly?
Short answer: no.

Longer answer: no, you have to use non-virtual methods for this. Those
methods could in turn call virtual methods if you need to customize behavior
in a fixed framework.
 
T

tshad

Jeroen Mostert said:
Short answer: no.

Longer answer: no, you have to use non-virtual methods for this. Those
methods could in turn call virtual methods if you need to customize
behavior in a fixed framework.
So I wouldn't be able to use the same method name - correct?

Thanks,

Tom
 
J

Jeroen Mostert

tshad said:
So I wouldn't be able to use the same method name - correct?
That's a separate issue -- you can overload names for nonvirtual methods by
using the "new" qualifier on a method, but because of the potential for
confusion, this isn't recommended except to work around compatibility
issues. There are select cases where using overloaded names can make for a
better design, but you'll have to decide for yourself whether yours is such
a case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top