Visual Basic Language Concepts
Overriding Properties and Methods

Derived classes inherit properties and methods defined in their base class. This is useful because it means you can reuse these items when appropriate for the class you are using. If the inherited member cannot be used "as is" you have the option of using the Overrides keyword to define a new implementation, provided that the property or method in the base class is marked with the Overridable keyword, or shadowing the member by redefining it in the derived class.

In practice, overridden members are often used to implement polymorphism. For more information about polymorphism see Polymorphism.

The following rules apply to overriding methods.

  • You can only override members that are marked with the Overridable keyword in their base class.
  • Properties and methods are NotOverridable by default.
  • Overridden members must have the same arguments as the inherited members from the base class.
  • The new implementation of a member can call the original implementation in the parent class by specifying MyBase before the method name.

Note   Overloading, overriding, and shadowing are similar concepts that can be easy to confuse. See Introduction to Objects in Visual Basic for more information.

Example

Suppose you want to define classes to handle payroll. You could define a generic Payroll class that contains a RunPayroll method that calculates payroll for a normal week. You could then use Payroll as a base class for a more specialized BonusPayroll class, which could be used when distributing employee bonuses.

The BonusPayroll class can inherit, and override, the PayEmployee method defined in the base Payroll class.

The following example defines a base class, Payroll, and a derived class, BonusPayroll, which overrides an inherited method, PayEmployee. A procedure, RunPayroll, creates and then passes a Payroll object and a BonusPayroll object to a function, Pay, that executes the PayEmployee method of both objects.

Const BonusRate As Decimal = 1.45
Const PayRate As Decimal = 14.75

Class Payroll
   Overridable Function PayEmployee(ByVal HoursWorked As Decimal, _
         ByVal PayRate As Decimal) As Decimal
      PayEmployee = HoursWorked * PayRate
   End Function
End Class

Class BonusPayroll
   Inherits Payroll
   Overrides Function PayEmployee(ByVal HoursWorked As Decimal, _
         ByVal PayRate As Decimal) As Decimal
      ' The following code calls the original method in the base 
      ' class, and then modifies the returned value.
      PayEmployee = MyBase.PayEmployee(HoursWorked, PayRate) * BonusRate
   End Function
End Class

Sub RunPayroll()
   Dim PayrollItem As Payroll = New Payroll()
   Dim BonusPayrollItem As New BonusPayroll()
   Dim HoursWorked As Decimal = 40

   MessageBox.Show("Normal pay is: " & _
   PayrollItem.PayEmployee(HoursWorked, PayRate))
   MessageBox.Show("Pay with bonus is: " & _
   BonusPayrollItem.PayEmployee(HoursWorked, PayRate))
End Sub

See Also

Overloaded Properties and Methods |Override Modifiers | Polymorphism | Shadowing

Page view tracker