VB.Net
  Home arrow VB.Net arrow Page 2 - MyClass - Implementing Polymorphism in VB....
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
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? 
VB.NET

MyClass - Implementing Polymorphism in VB.Net
By: Softwaremaker
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 323
    2003-11-20

    Table of Contents:
  • MyClass - Implementing Polymorphism in VB.Net
  • Code

  • 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


    MyClass - Implementing Polymorphism in VB.Net - Code


    (Page 2 of 2 )

    I will replicate the exact same code, as first published in the original tip, and I will place the keyword in the appropriate place.

    Lets see some code below:

    Class Parent
    Public Overridable Sub p1()
    Console.Write("Parent.p1")
    End Sub
    Public Sub p2()
    MyClass.p1()
    'Implementing keyword MyClass here tells all derived classes to refer to the base / abstract class wherein the keyword MyClass appears
    End Sub
    End Class

    Class Child
    Inherits Parent
    Public Overrides Sub p1()
    Console.Write("Child.p1")
    MyBase.p2()
    End Sub
    End Class

    Sub Main()
    Dim p As Parent
    Dim c As Child
    p = New Parent()
    p.p1() 'OK
    p.p2() 'OK
    p = New Child()
    p.p1() 'stack overflow error is prevented

    If MyClass is not implemented above in the base class, you will get a stack overflow error. So why does the stack overflow occur?

    • Parent.p2() calls Parent.p1()
    • Parent.p1() is polymorphic because it can be overridden
    • Child.p1() overrides Parent.p1() so any calls to Parent.p1() will actually call Child.p1() if you have an instance of class Child
    • Child.p1() calls MyBase.p2()
    • MyBase.p2() is actually Parent.p2()

    Now if you are still with me, the pitfall comes when you have an instance of class Child and call procedures p1 and p2. Calling p1 produces the following execution flow:

    Child.p1 -> Parent.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> etc.

    And the cycle repeats until we have no stack space left. Calling p2 produces the following execution flow:

    Child.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> etc.

    Implementing the keyword MyClass in the base class tells all derived classes to use the method in the base class itself and, therefore, a stack overflow error is prevented.

    p.p2() 'stack overflow error is prevented
    c = New Child()
    c.p1() 'stack overflow error is prevented
    c.p2() 'stack overflow error is prevented
    End Sub

    I hope this will help VB.Net developers along the way to develop software the object-oriented way.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    VB.NET ARTICLES

    - MyClass - Implementing Polymorphism in VB.Net
    - Building a News Ticker Using VB.Net
    - Everything You Wanted to Know About Forms In...
    - Building Assemblies with VB.Net
    - Simple VB.NET Notify Icon with Panel Applica...
    - Regular Expressions in .NET
    - String Encryption With Visual Basic .NET
    - Deploying Applications in VB.NET: Part 1/2
    - Watching Folder Activity in VB.NET
    - Creating A Windows Service in VB.NET
    - Implementing The Google Web Service In VB.NET
    - Migrating to Visual Basic.NET from Visual Ba...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek