VB.Net/Class/Polymorphism
Demonstrate polymorphism in Point-Circle-Cylinder hierarchy
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) " instantiate Point, Circle and Cylinder objects 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) " instantiate array of base-class references Dim arrayOfShapes As CShape() = New CShape(2) {} " arrayOfShapes(0) refers to Point object arrayOfShapes(0) = point " arrayOfShapes(1) refers to Circle object arrayOfShapes(1) = circle " arrayOfShapes(2) refers to Cylinder object arrayOfShapes(2) = cylinder Console.WriteLine( point.Name & ": " & _ point.ToString() & vbCrLf & circle.Name & ": " & _ circle.ToString() & vbCrLf & cylinder.Name & _ ": " & cylinder.ToString() ) Dim shape As CShape For Each shape In arrayOfShapes Console.WriteLine( vbCrLf & vbCrLf & shape.Name & ": " & _ shape.ToString() & vbCrLf & "Area = " & _ String.Format("{0:F}", shape.Area) & vbCrLf & _ "Volume = " & String.Format("{0:F}", shape.Volume) ) Next End Sub
End Class Public MustInherit Class CShape
" return shape area Public Overridable Function Area() As Double Return 0 End Function " Area " return shape volume Public Overridable Function Volume() As Double Return 0 End Function " Volume " overridable method that should return shape name Public MustOverride ReadOnly Property Name() As String
End Class " CShape
Public Class Circle
Inherits Point " Circle Inherits from class Point Private mRadius As Double " default constructor Public Sub New() " implicit call to Point constructor occurs here Radius = 0 End Sub " New " constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double) " use MyBase reference to Point constructor explicitly MyBase.New(xValue, yValue) Radius = radiusValue End Sub " New " property Radius Public Property Radius() As Double Get Return mRadius End Get Set(ByVal radiusValue As Double) If radiusValue >= 0 Then " mRadius must be nonnegative mRadius = radiusValue End If End Set End Property " Radius " calculate Circle diameter Public Function Diameter() As Double Return mRadius * 2 End Function " Diameter " calculate Circle circumference Public Function Circumference() As Double Return Math.PI * Diameter() End Function " Circumference " calculate Circle area Public Overrides Function Area() As Double Return Math.PI * mRadius ^ 2 End Function " Area " return String representation of Circle Public Overrides Function ToString() As String " use MyBase to return Circle String representation Return "Center= " & MyBase.ToString() & _ "; Radius = " & mRadius End Function " ToString " override property Name from class Point Public Overrides ReadOnly Property Name() As String Get Return "Circle" End Get End Property " Name
End Class Public Class Cylinder
Inherits Circle " Cylinder inherits from class Circle Private mHeight As Double " default constructor Public Sub New() " implicit call to Circle constructor occurs here Height = 0 End Sub " New " four-argument constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double, _ ByVal heightValue As Double) " explicit call to Circle constructor MyBase.New(xValue, yValue, radiusValue) Height = heightValue " set Cylinder height End Sub " New " property Height Public Property Height() As Double Get Return mHeight End Get " set Cylinder height if argument value is positive Set(ByVal heightValue As Double) If heightValue >= 0 Then " mHeight must be nonnegative mHeight = heightValue End If End Set End Property " Height " override method Area to calculate Cylinder surface area Public Overrides Function Area() As Double Return 2 * MyBase.Area + MyBase.Circumference * mHeight End Function " Area " calculate Cylinder volume Public Overrides Function Volume() As Double Return MyBase.Area * mHeight End Function " Volume " convert Cylinder to String Public Overrides Function ToString() As String Return MyBase.ToString() & "; Height = " & mHeight End Function " ToString " override property Name from class Circle Public Overrides ReadOnly Property Name() As String Get Return "Cylinder" End Get End Property " Name
End Class Public Class Point
Inherits CShape " Point inherits from MustInherit class CShape " point coordinate Private mX, mY As Integer " default constructor Public Sub New() " implicit call to Object constructor occurs here X = 0 Y = 0 End Sub " New " constructor Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer) " implicit call to Object constructor occurs here X = xValue Y = yValue End Sub " New " property X Public Property X() As Integer Get Return mX End Get Set(ByVal xValue As Integer) mX = xValue " no need for validation End Set End Property " X " property Y Public Property Y() As Integer Get Return mY End Get Set(ByVal yValue As Integer) mY = yValue " no need for validation End Set End Property " Y " return String representation of Point Public Overrides Function ToString() As String Return "[" & mX & ", " & mY & "]" End Function " ToString " implement MustOverride property of class CShape Public Overrides ReadOnly Property Name() As String Get Return "Point" End Get End Property " Name
End Class
</source>
PolyMorphism At Work
<source lang="vbnet"> Imports System Imports System.Collections
Public Class MainClass
Shared Sub Main() Dim e1 As New Employee("Employee", 5) Dim e2 As New Programmer("Programmer", 15) Dim e3 As New Archictect("Archictect", 15) Dim ourEmployees(2) As Employee ourEmployees(0) = e1 ourEmployees(1) = e2 ourEmployees(2) = e3 Dim anEmployee As Employee For Each anEmployee In ourEmployees anEmployee.RaiseSalary(0.1D) Console.WriteLine(anEmployee.TheName & " salary now is " & anEmployee.Salary()) Next End Sub
End Class Public Class Employee
Private m_Name As String Private m_Salary As Decimal Public Sub New(ByVal theName As String, ByVal curSalary As Decimal) m_Name = TheName m_Salary = curSalary End Sub Public ReadOnly Property TheName() As String Get Return m_Name End Get End Property Public ReadOnly Property Salary() As Decimal Get Return MyClass.m_Salary End Get End Property Public Overridable Overloads Sub RaiseSalary(ByVal Percent As Decimal) m_Salary = (1 + Percent) * m_Salary End Sub
End Class Public Class Programmer
Inherits Employee Public Sub New(ByVal theName As String, ByVal curSalary As Decimal) MyBase.New(theName, curSalary) End Sub Public Overloads Overrides Sub RaiseSalary(ByVal Percent As Decimal) MyBase.RaiseSalary(1.2D * Percent) End Sub
End Class Public Class Archictect
Inherits Programmer Public Sub New(ByVal theName As String, ByVal curSalary As Decimal) MyBase.New(theName, curSalary) End Sub Public Overloads Overrides Sub RaiseSalary(ByVal Percent As Decimal) MyBase.RaiseSalary(2D * Percent) End Sub
End Class
</source>