Closer Look: Overriding Members

In this lesson, you will learn how to override a member of a derived class.

In the previous lesson, you learned how to inherit from a base class and extend the derived class with new properties. In addition to adding new properties or methods to a derived class, you may also want to change, or override, the behavior of existing properties or methods.

For example, you might create a Truck class that is derived from a Car class that has a StartEngine method. If the truck has a diesel engine, the process of starting the engine might differ from that of a car; in this case you would want to override the StartEngine method to better fit the truck.

Overriding Properties and Methods

By default, properties and methods in a class cannot be overridden. To allow a derived class to override a property or method, it must be marked as overridable by declaring it with the Overridable keyword.

Public Overridable Property EngineType As String

Public Overridable Sub StartEngine(ByVal EngineType As String)

When inheriting from a base class, properties and methods that are marked as Overridable can be used as is, or they can be modified to suit your needs by declaring them with the Overrides keyword.

Public Overrides Property EngineType As String

Public Overrides Sub StartEngine(ByVal EngineType As String)

In the Players class that you created in the previous lesson, you might want to override the FullName method to include the player's number and to eliminate the code that returns a middle name.

Try It!

To override the FullName method

  1. Open the Persons project that you created in the previous lesson. If you did not save it, go back to the previous lesson, Building a Class from an Existing Class: Using Inheritance, and complete the procedures.

  2. In Solution Explorer, select the Persons.vb node, and then on the View menu, choose Code.

  3. In the Code Editor, modify the declaration for the FullName method as follows.

    Public Overridable Function FullName() As String
    
  4. In Solution Explorer, select the Players.vb node, and then on the View menu, choose Code.

  5. In the Code Editor, add the following code to the class.

    Public Overrides Function FullName() As String
        FullName = FirstName & " " & LastName & ", #" & numberValue
    End Function
    
  6. In Solution Explorer, select the Form1.vb node in the PlayerTest project, and then on the View menu, choose Code.

  7. In the Code Editor, modify the Button1_Click event code as follows.

    MsgBox(player1.Position & " " & player1.FullName & _
          " is now at bat.")
    
  8. Modify the Button2_Click event code as follows.

    MsgBox(player2.Position & " " & player2.FullName & _
          " is on deck.")
    
  9. Press F5 to run the program, and click each button to display the results.

    Notice that the results are the same as before, even though you are now using the overridden FullName method.

  10. On the File menu, choose Save All.

Next Steps

In this lesson, you learned how to override a method. In the next lesson, you will learn about using collections to manage groups of similar objects.

Next Lesson: Using Collections to Manage Multiple Objects

See Also

Tasks

Building a Class from an Existing Class: Using Inheritance

Reference

Overridable

Overrides