Posted by: Sourav | August 9, 2016

Polymorphism example in VB


‘Option Strict On
Imports System

Module Module1

Sub Main()
‘  Dim employees As New List(Of Employee)
Dim employees(3) As Employee

‘  employees.Add(New Employee)
employees(0) = New Employee
employees(1) = New FullTimeEmployee
employees(2) = New PartTimeEmployee
employees(3) = New TempEmployee

’employees.Add(New FullTimeEmployee)
’employees.Add(New PartTimeEmployee)
’employees.Add(New TempEmployee)
‘  Dim e As Employee = New Employee
Dim i As Integer = 1

For Each e As Employee In employees
‘If e IsNot Nothing Then
e.printfullname()

‘ End If
‘  Console.WriteLine(i)
‘  i += 1

Next
‘ Console.WriteLine()
‘ Console.WriteLine(“Doing the same thing using simple for loop”)
‘ Console.WriteLine()

For i = 1 To UBound(employees)
‘   employees(i).printfullname()
Next
Console.ReadLine()

End Sub

End Module

 

 

Public Class Employee
Public FirstName As String = “FN”
Public LastName As String = “LN”
Overridable Sub printfullname()
Console.WriteLine(FirstName + ” ” + LastName)

End Sub
End Class

 

 

 

Public Class FullTimeEmployee
Inherits Employee
Overrides Sub printfullname()
Console.WriteLine(FirstName + ” ” + LastName + ” Full Time”)

End Sub

End Class

 

 

 

Public Class PartTimeEmployee
Inherits Employee
Overrides Sub printfullname()
Console.WriteLine(FirstName + ” ” + LastName + ” Part Time”)

End Sub
End Class

 

 

 

Public Class TempEmployee
Inherits Employee

Overrides Sub printfullname()
Console.WriteLine(FirstName + ” ” + LastName + ” Temp”)

End Sub
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: