1

Suppose I had the following code:

Class NormalEmployee
     Protected pay As Decimal;
     Protected Shared basePay As Decimal = 300D
     Protected Overridable Sub UpdatePay()
         pay = basePay + .....do something......
     End Sub
End Class

Class SeniorNormalEmployee
  Inherits Normal Employee
    Protected Shared Shadows basePay As Decimal = 500D;
    Protected Overrides Sub UpdatePay()
         pay = basePay + .....do something different....
    End Sub
End Class

Function Main() As Integer
    Dim newEmployee As NormalEmployee = New SeniorNormalEmployee()
    newEmployee.CalculatePay()
    return 0
End Function

I know that due to polymorphism, the CalculatePay() from my base class will be called. My question is: why does CalculatePay() use the basePay from the base class and not the derived class? The object is being stored inside a base class "container", so even though it uses the derived classes version of the method, when it goes to check the basePay shouldn't it look at the base class's version?

Furthermore, is this behavior the same when calling shadowed methods from an overrides method? Is there any way to make a field "Overridable"?

Thanks for any help!

powrHouse
  • 11
  • 1
  • I should also add that I know the program design isn't phenomenal, however this is the design I'm stuck with due to assignment requirements. Also, I just whipped up this code as a shortened example, so my apologies if there are any errors or missing syntax. – powrHouse Mar 17 '15 at 04:44

1 Answers1

0

The problem is that using the Shadows keyword makes your code use the varible of the base class, when you reference to the base class. Since you used polymorphyism and your newEmployee is defined as NormalEmployee, the basepay is the one of the base class. For more information, check here.

Community
  • 1
  • 1
Eminem
  • 870
  • 5
  • 20
  • This is what I thought would happen, but it is NOT what is happening in my code. Even though my `newEmployee` is defined as a `NormalEmployee` and uses the `SeniorNormalEmployee`'s version of `CalculatePay()`, it uses the *derived* class's `basePay`. This seems abnormal to me as `newEmployee` is of type `NormalEmployee`, so even though it uses the derived class's method thanks to polymorphism, I would think it would use the base class's version of `basePay`, which it doesn't. – powrHouse Mar 17 '15 at 18:13
  • My hunch is that when polymorphism kicks in and determines that the object should use the derived class version of `CalculatePay()`, it implicitly casts the object to the derived type, as it would have to if the derived `CalculatePay()` used any methods or fields that are exclusive to the derived type. Because of this internal cast, the derived class's `basePay` is retrieved instead of the base class's. – powrHouse Mar 17 '15 at 18:16
  • Have you tried not to use the Shadows keyword for the derived basePay? What will be the reuslt of that? – Eminem Mar 19 '15 at 11:24
  • Does the same thing, `Shadows` is the default behavior, the keyword just specifies to the compiler that you're aware that it's happening so that it doesn't generate a notice in the IDE. – powrHouse Mar 19 '15 at 16:21