IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
VB.Net
Search VB.Net Group:

Advanced
-Collapse +Expand VB.Net To/From
To/FromCODEGuides
-Collapse +Expand VB.Net Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingVB.NetOOP   Print This     
  From the November 2015 Issue of Prestwood eMag
 
VB.Net OOP:
VB.Net Overriding (Overridable, Overrides)
 
Posted 10 years ago on 3/8/2009
VB.Net Code Snippet:
 A flashcard from our VB.Net Flashcards Library
 A code snippet from our VB.Net Code Snippets Page
 Tags: VB.Net , Overriding

KB101947



General Info: Method Overriding

Where you define or implement a virtual method in a parent class and then replace it in a descendant class.

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.

In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.

VB.Net Overriding

In VB.Net, you specify a virtual method with the Overridable keyword in a parent class and extend (or replace) it in a descendant class using the Overrides keyword.

Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().

Syntax Example:
Public Class Robot
Public Overridable Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
  
Public Class Cyborg
Inherits Robot
  
  Public Overrides Sub Speak()
MessageBox.Show("hi")
End Sub
End Class

Override Details

  • You cannot override a regular non-virtual method, nor a static method.
  • The first version of the parent method must be Overridable or MustOverride.
  • You can override any parent method marked Overridable, MustOverride, or Overrides (already overridden).
  • The methods must have the same signature.
  • The methods must have the same visibility (the same access level).
  • Use the base keyword to refer to the parent class as in MyBase.SomeMethod().

Working VB.Net 2008 Override Example

The following code assumes a Windows application with a single form with a button. It demonstrates using Overridable and Overrides to override a parent method in a descendant class.

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyRobot As Robot
    Dim MyCyborg As Cyborg
  
    MyRobot = New Robot
    MyCyborg = New Cyborg
  
    MyRobot.Speak()
    MyCyborg.Speak()
  End Sub
End Class
  
Public Class Robot
  Public Overridable Sub Speak()
    MessageBox.Show("Robot says hi")
  End Sub
End Class
  
Public Class Cyborg
  Inherits Robot
  
  Public Overrides Sub Speak()
    MessageBox.Show("hi")
  End Sub
End Class

Hiding a Method with Shadows

Use the Shadows keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using Shadows but you will get a compiler warning. Using Shadows will suppress the warning.

The Shodaws and Overrides modifiers have different meanings. The Shadows modifier creates a new member with the same name, signature, and visibility and hides the original member. The Overrides modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.

Avoid Introducing New Members: Sometimes there are clear reasons to use Shadows to introduce a new method with with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate.

Public Class Robot
  Public Sub Speak()
    MessageBox.Show("Robot says hi")
  End Sub
End Class
  
Public Class Cyborg
  Inherits Robot
  
  Public Shadows Sub Speak()
    MessageBox.Show("hi")
  End Sub
End Class

Calling the Parent Class Version with MyBase

A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the MyBase keyword to refer to the parent class as in MyBase.SomeMethod().

Public Class Robot
  Public Overridable Sub Speak()
    MessageBox.Show("Robot says hi")
  End Sub
End Class
  
Public Class Cyborg
  Inherits Robot
  
  Public Overrides Sub Speak()
    MyBase.Speak()
    MessageBox.Show("hi")
  End Sub
End Class

 

More Info

Definition:  Method Overriding

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1261A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101947 Counter
25724
Since 3/8/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]