Demonstrate polymorphism in Point-Circle-Cylinder hierarchy. : Polymorphism « Class Module « VB.Net Tutorial






Class CTest2
   Shared Sub Main()
      Dim point As New Point(7, 11)
      Dim circle As New Circle(22, 8, 3.5)
      Dim cylinder As New Cylinder(10, 10, 3.3, 10)

      Dim arrayOfShapes As Shape() = New Shape(2) {}

      arrayOfShapes(0) = point

      arrayOfShapes(1) = circle

      arrayOfShapes(2) = cylinder

      Console.WriteLine(point.Name & ": " & _
         point.ToString() & vbCrLf & circle.Name & ": " & _
         circle.ToString() & vbCrLf & cylinder.Name & _
         ": " & cylinder.ToString())

      Dim shape As Shape

      For Each shape In arrayOfShapes
         Console.WriteLine(shape.Name & ": " & _
            shape.ToString() & vbCrLf & "Area = " & _
            String.Format("{0:F}", shape.Area) & vbCrLf & _
            "Volume = " & String.Format("{0:F}", shape.Volume))
      Next

   End Sub ' Main

End Class

Public MustInherit Class Shape

   Public Overridable Function Area() As Double
      Return 0
   End Function

   Public Overridable Function Volume() As Double
      Return 0
   End Function

   Public MustOverride ReadOnly Property Name() As String

End Class


Public Class Point
   Inherits Shape 
   Private mX, mY As Integer

   Public Sub New()
   End Sub ' New

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
   End Sub ' New

   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function ' ToString

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Point"
      End Get

   End Property ' Name

End Class

Public Class Circle
   Inherits Point 

   Private mRadius As Double

   Public Sub New()
   End Sub ' New

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)
      MyBase.New(xValue, yValue)
   End Sub ' New

   Public Overrides Function ToString() As String
      Return "Center= " & MyBase.ToString() & _
         "; Radius = " & mRadius
   End Function ' ToString

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Circle"
      End Get

   End Property ' Name

End Class

Public Class Cylinder
   Inherits Circle

   Protected mHeight As Double

   Public Sub New()
   End Sub ' New

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double, _
      ByVal heightValue As Double)

      MyBase.New(xValue, yValue, radiusValue)
   End Sub ' New

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Cylinder"
      End Get

   End Property ' Name

End Class
Point: [0, 0]
Circle: Center= [0, 0]; Radius = 0
Cylinder: Center= [0, 0]; Radius = 0
Point: [0, 0]
Area = 0.00
Volume = 0.00
Circle: Center= [0, 0]; Radius = 0
Area = 0.00
Volume = 0.00
Cylinder: Center= [0, 0]; Radius = 0
Area = 0.00
Volume = 0.00








6.15.Polymorphism
6.15.1.Polymorphic Behaviour
6.15.2.Inheritance and polymorphism
6.15.3.Demonstrate polymorphism in Point-Circle-Cylinder hierarchy.
6.15.4.Late binding