Overriding
Defining an inheritance hierarchy is all about defining the types in a system from most general to most specific. With inheritance, however, a derived type can only add new members to those it inherits from its base type. Sometimes, though, a derived type may want to change the behavior of members that it inherits from a base type. For example, the base class Person may define a Print method that prints information about the class.
Class Person Public Name As String Public Address As String Public City As String Public State As String Public ZIP As String Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) End Sub End Class Class Employee Inherits Person Public Salary As Integer End Class
In this example, though, calling the method Employee.Print will only print the name and address of an employee, not the employee's salary. There is no way, using inheritance, to change the inherited implementation of Person.Print.
Changing the implementation of derived methods is possible, however, through overriding. A base class can declare that a particular method or methods are Overridable, which means that a derived class can replace the implementation that the base class provides. For example:
Class Person Public Name As String Public Address As String Public City As String Public State As String Public ZIP As String Overridable Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) End Sub End Class Class Employee Inherits Person Overrides Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) Console.WriteLine("Salary = " & Salary) End Sub Public Salary As Integer End Class
In this example, the Employee class overrides Person's implementation of the Print method with its own version of the Print method that prints the salary as well as the employee's name and address.
One interesting thing to note is that when a type overrides a base type's member, that override applies to all instances of the type, no matter what their stated type is. In other words, Employee's implementation of Print is the one that will be called on an Employee instance, even if it is typed as a Person. The following example:
Module Test Sub Main() Dim e As Employee = New Employee() Dim p As Person e.Name = "John Doe" e.Address = "123 Main St." e.City = "Toledo" e.State = "OH" e.ZIP = "48312" e.Salary = 43912 p = e p.Print() End Sub End Module
will print this:
John Doe 123 Main St. Toledo, OH 48312 Salary = 43912
even though the type of the variable that Print is being called on is Person instead of Employee.
Properties can also be overridden. The following is an example of overriding a property.
Class Order Public Cost As Double Public Quantity As Integer Public Overridable ReadOnly Property Total() As Double Get Return Cost * Quantity End Get End Property End Class Class ForeignOrder Inherits Order Public ConversionRate As Double Public Overrides ReadOnly Property Total() As Double Get Return MyBase.Total * ConversionRate End Get End Property End Class
When you are overriding a read-write property, both the Get and the Set accessors must be provided, even if you wish to override only one of them. Only methods and properties can be overridden, and they can be overridden only if they specify the Overrides keyword in their declaration. This is to prevent programmers from accidentally letting derived classes override methods that they did not intend to be overridden.
Only accessible members from a base type can be overridden. Thus, a Friend Overridable method cannot be overridden outside the assembly it is declared in. (It is not valid to declare a method Private Overridable, because no derived type could override such a method.) When you are overriding a method, the access of the overriding method must be the same as the method being overridden.
When you are overriding a Protected Friend method from a derived class that is not in the same assembly as the class, the overriding method specifies just Protected instead of Protected Friend.
Sometimes it is desirable to override a method but prevent any further derived classes from overriding the method. Adding the NotOverridable keyword to a method that is overriding another method prevents any further derived classes from overriding the method.
MyBase and MyClass
In the example in the previous section, Employee.Print had to supply the entire implementation of Person.Print so that the name and address would still be printedif it hadn't done that, only the salary would have been printed. In this situation, the keyword MyBase can be used to call the methods of the base class, allowing Employee.Print to call Person.Print. Calling methods off of MyBase calls the base class's implementation of a method, even if the derived class has overridden it. So the example could be rewritten as follows.
Class Person Public Name As String Public Address As String Public City As String Public State As String Public ZIP As String Overridable Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) End Sub End Class Class Employee Inherits Person Overrides Sub Print() MyBase.Print() Console.WriteLine("Salary = " & Salary) End Sub Public Salary As Integer End Class
The result would be the same: First, Person.Print would be called to print the name and address, and then Employee.Print would print the salary.
Sometimes it is desirable to call the particular implementation of a method that your class provides, regardless of whether the instance might be of a type that overrides it. Qualifying the method call with the keyword MyClass will always call the containing class's implementation of a method, ignoring any further implementation. The following example:
Class Person Public Name As String Public Address As String Public City As String Public State As String Public ZIP As String Sub CallPrint() Print() End Sub Sub CallMyClassPrint() MyClass.Print() End Sub Overridable Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) End Sub End Class Class Employee Inherits Person Overrides Sub Print() Console.WriteLine(Name) Console.WriteLine(Address) Console.WriteLine(City & ", " & State & " " & ZIP) Console.WriteLine("Salary = " & Salary) End Sub Public Salary As Integer End Class Module Test Sub Main() Dim e As Employee = New Employee() Dim p As Person e.Name = "John Doe" e.Address = "123 Main St." e.City = "Toledo" e.State = "OH" e.ZIP = "48312" e.Salary = 43912 p = e Console.WriteLine("CallPrint:") p.CallPrint() Console.WriteLine() Console.WriteLine("CallMyClassPrint:") p.CallMyClassPrint() End Sub End Module
will print the following information.
CallPrint: John Doe 123 Main St. Toledo, OH 48312 Salary = 43912 CallMyPrint: John Doe 123 Main St. Toledo, OH 48312
When the method Person.CallPrint calls the overridable Print method, what Print method ends up getting called depends on the actual type instance at runtime. Since the instance in this case is actually an Employee, Person.CallPrint ends up calling Employee.Print. However, because CallMyClassPrint qualifies the call to Print with MyClass, it always calls Person.Print, even if the instance is a more derived class.