Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - Overloading and Overriding in Visual Basic...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Overloading and Overriding in Visual Basic.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-07-30

    Table of Contents:
  • Overloading and Overriding in Visual Basic.NET 2005
  • A sample class with method overloading
  • A sample class with constructor overloading
  • Method overriding in Visual Basic 2005
  • Multi-level method overriding in Visual Basic 2005 inheritance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Lose your application development headaches. Start developing and deploying applications with Advantage Database Server today. Download a 30-day trial for Free!

    Overloading and Overriding in Visual Basic.NET 2005 - Method overriding in Visual Basic 2005
    (Page 4 of 5 )

    In all of the previous sections, we worked with only "overloading."  This section focuses on "overriding." 

    Before proceeding further, please note that there is no relationship or similarity between "overloading" and "overriding." They are completely different. To demonstrate "overriding," one needs to work with inheritance. If you are new to inheritance in Visual Basic.NET, I strongly suggest that you go through the following article:

    Inheritance with VB.NET 2005

    Let us start with defining two simple classes as follows:

    Public Class Parent

      Public Sub DispMsg()

        MessageBox.Show("From Parent")

      End Sub

    End Class

     

    Public Class Child

      Inherits Parent

      Public Sub DispMsg()

        MessageBox.Show("From child")

      End Sub

    End Class

    From the above, we have two classes, "Parent" and "Child."  The "Child" class gets inherited from the "Parent" class.  The parent class has a method named "DispMsg," and the same method is redefined in the child with the same name.

    If we create a parent object (instance), we will have only one method and we can call it without any problem. But if we create a child object, we will have two methods -- one inherited from the parent -- with the same name and signature. Thus the program gives out a warning and defaults to the child method whenever "DispMsg" is called from the child object.

    To eliminate this warning, we should either "override" or "shadow" the parent (or inherited) method.  I shall cover the "shadow" technique in my upcoming articles.  For now, let us concentrate on the "override" technique.

    The following are the modifications to the previous classes which are needed to implement overriding:

    Public Class Parent

      Public Overridable Sub DispMsg()

        MessageBox.Show("From Parent")

      End Sub

    End Class

     

    Public Class Child

      Inherits Parent

      Public Overrides Sub DispMsg()

        MessageBox.Show("From child")

      End Sub

    End Class

    To override (or redefine) an inherited method in the child class, the same method must be declared with "overridable" in the parent and with "overrides" in the child. To test the above classes, you can use the following code:

    Dim objP As New Parent

    objP.DispMsg() 'displays "From Parent"

     

    Dim obj As New Child

    obj.DispMsg() 'displayes "From Child"

    Please note that even if you use a parent reference with the child object, the result would be the same as accessing the child member.  Try testing it out using the following:

    CType(obj, Parent).DispMsg()'displayes "From Child"

    DirectCast(obj, Parent).DispMsg()'displayes "From Child"

    Dim RefParent As Parent = obj

    RefParent.DispMsg()'displayes "From Child"

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys,This is an article for the persons who always confuse about...
     

    VISUAL BASIC.NET ARTICLES

    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic
    - Manipulating Forms with the Windows Forms Li...
    - Basics of the Windows Forms Library
    - Forms, Controls, and Other Useful Objects
    - Implementing OOP to Develop Database Oriente...
    - Using Themes and Skins for Personalization w...
    - A Deeper Look at Personalization using Visua...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway