Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Polymorphism using Abstract Classes in Vis...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

Polymorphism using Abstract Classes in Visual Basic.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 20
    2007-08-01

    Table of Contents:
  • Polymorphism using Abstract Classes in Visual Basic.NET 2005
  • Life without the Abstract class in Visual Basic 2005: continued
  • Defining and implementing abstract classes in Visual Basic 2005: abstract class
  • Defining and implementing abstract classes in Visual Basic 2005: child class
  • Defining and implementing abstract classes in Visual Basic 2005: child classes continued
  • Making use of Polymorphism using abstract classes in Visual Basic 2005

  • 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


    Polymorphism using Abstract Classes in Visual Basic.NET 2005 - Life without the Abstract class in Visual Basic 2005: continued


    (Page 2 of 6 )

    In the previous section, a "Rectangle" class was defined.  In this section, two more classes are introduced as follows:

    Public Class EquiTriangle

      Private _h As Double

      Private _b As Double

     

      Public Sub New(ByVal breadth As Double, ByVal height As Double) 'code removed for clarity

     

      Public Property H() As Double 'code removed for clarity

     

      Public Property B() As Double 'code removed for clarity

     

      Public Function GetArea() As Double

        Return (1.0 / 2.0) * _b * _h

      End Function

    End Class

     

    Public Class Square

      Private _s As Double

      Public Sub New(ByVal side As Double)

        Me.S = side

      End Sub

     

      Public Property S() As Double

        Get

          Return _s

        End Get

        Set(ByVal value As Double)

          _s = value

        End Set

      End Property

     

      Public Function GetArea() As Double

        Return _s * _s

      End Function

    End Class

    If you observe the three classes "Rectangle," "EquiTriangle" and "Square," you will see several similarities.  First of all, "GetArea" is available in all the classes (and it should be available for any of the shapes). In both "Rectangle" and "EquiTriangle," there exist two properties with almost the same signature along with constructors.

    The code to test the above classes would be the following:

        Dim r As New Rectangle(10, 20)

        Dim s As New Square(5)

        Dim t As New EquiTriangle(5, 20)

        MessageBox.Show("Area of rectangle: " & r.GetArea)

        MessageBox.Show("Area of square: " & s.GetArea)

        MessageBox.Show("Area of triangle: " & t.GetArea)

    Let us say I want the developers on my team to implement "GetArea" with the same signature as available in "Rectangle" (just to be consistent among all classes).  If the developers define their own methods like "GetEquiTriangleArea" or "GetSquareArea," the code would not give any compilation error (or warning) and somehow I lose consistency.

    To maintain signature consistency (without implementation) in our code and to enforce such rules, we can implement "abstract" classes, as defined in the next section.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hai,This is my new article (adding to the series of object oriented programming...
       · I like the way the author explains the concept with simple examples.I would like to...
       · I liked the way in which the Author of this Article has taken a fimiliar and most...
       · Fabulous tutorial. Could not be clearer. Thank you.
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT