![]() ![]() | ||
Overloading, overriding, and shadowing are also important concepts in Visual Basic OOP. These techniques allow you to create multiple members with the same name. We've already discussed them in the previous chapter; here's how they work in overview:
Overloaded—Overloaded members provide different versions of a property or method that have the same name, but that accept different number of parameters (or parameters of different types).
Overridden—Overridden properties and methods are used to replace an inherited property or method. When you override a member from a base class, you replace it. Overridden members must accept the same data type and number of arguments.
Shadowed—Shadowed members are used to create a local version of a member that has broader scope. You also can shadow a type with any other type. For example, you can declare a property that shadows an inherited method with the same name.
We saw overloading in the previous chapter, although we'll have a little more to say about it here. In particular, there is an Overloads keyword that you can use to indicate that you're overloading a method or property. You don't need to use Overloads when you're overloading members of the same class, but it becomes important when you're working with inheritance—see "Overloading Base Class Members" in this chapter.
What's overriding? If an inherited property or method needs to behave differently in the derived class, it can be overridden; that is, you can define a new implementation of the method in the derived class. The following modifiers are used to control how properties and methods are overridden:
Overridable— Allows a property or method in a class to be overridden.
Overrides— Overrides an Overridable property or method.
NotOverridable— Prevents a property or method from being overridden. Note that public methods are NotOverridable by default.
MustOverride— Requires that a derived class override the property or method. MustOverride methods must be declared in MustInherit classes.
Let's see an example of overriding at work. In the Inheritance example on the CD-ROM, the base class is Animal, and when you call its Breathing method, it displays "Breathing…". However, if you were to derive a class named Fish from Animal, that wouldn't be quite appropriate—you might want this method to display something like "Bubbling…" instead. To do that, you can override the Animal class's Breathing method in the Fish class. All that takes is to mark the Animal class's Breathing method as Overridable, and to use the Overrides keyword when defining the Breathing method in the Fish class:
Public Class Animal Protected MainForm As Form1 Public Sub New(ByVal form1 As Form1) MainForm = form1 End Sub Public Overridable Sub Breathing() MainForm.TextBox1.Text = "Breathing..." End Sub End Class Public Class Fish Inherits Animal Public Sub New(ByVal form1 As Form1) MyBase.New(form1) End Sub Public Overrides Sub Breathing() MyBase.MainForm.TextBox1.Text = "Bubbling..." End Sub End Class
Now I can declare a Fish object named jaws and when I use its overriden Breathing method, you'll see "Bubbling…", not "Breathing…":
Dim jaws As Fish Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click jaws = New Fish(Me) jaws.Breathing() End Sub
You can see this code at work in the Inheritance example on the CD-ROM in Figure 12.2.
Here's an interesting OOP fact—if you declare an object of the Animal class, you can assign any object of a class derived from Animal, such as the Dog class, to that object:
Dim obj As Animal Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click obj = New Dog(Me) ⋮ End Sub
Now you can use all the Animal class's members with the obj object—but you can't use any members that Dog has that Animal doesn't (because obj is declared as an Animal object). For example, the Dog class implements its own Barking method, so Visual Basic won't let you use obj.Barking.
So what happens if you use a derived class that has overridden some base class members? For example, the Fish class overrides the Animal class's Breathing method, so if you use this code, will you see "Breathing…" or "Bubbling…"?
Dim obj As Animal Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click obj = New Fish(Me) obj.Breathing() End Sub
Since obj is an Animal object, you might expect to see "Breathing…", but in fact, you'll see "Bubbling…". That's because all methods are virtual in Visual Basic OOP, which means that objects use the latest overridden method. In this case, that means the Fish class's Breathing method is used, not the Animal base class's Breathing method. (This fact forms the basis of polymorphism, which we'll also go into in this chapter.)
Tip |
If you are familiar with OOP, it might interest you to know that you also can create pure virtual methods, called abstract methods, with the Visual Basic MustInherit keyword. An abstract method can't be used in a base class, but must be implemented in a derived class. |
![]() ![]() | ||