VB.Net/Class/Override
Overrides method from super(parent) class
Imports System
Public Class MainClass
Shared Sub Main()
Dim win As New Window(1, 2)
Dim lb As New ListBox(3, 4, "Stand alone list box")
Dim b As New Button(5, 6)
win.DrawWindow( )
lb.DrawWindow( )
b.DrawWindow( )
Dim winArray(3) As Window
winArray(0) = New Window(1, 2)
winArray(1) = New ListBox(3, 4, "List box in array")
winArray(2) = New Button(5, 6)
Dim i As Integer
For i = 0 To 2
winArray(i).DrawWindow( )
Next i
End Sub
End Class
Public Class Window
Public Sub New(ByVal top As Integer, ByVal left As Integer)
Me.top = top
Me.left = left
End Sub
Public Overridable Sub DrawWindow( )
Console.WriteLine("Window: drawing Window at {0}, {1}", top, left)
End Sub
Protected top As Integer
Protected left As Integer
End Class
Public Class ListBox
Inherits Window
Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal contents As String)
MyBase.New(top, left)
listBoxContents = contents
End Sub
Public Overrides Sub DrawWindow( )
MyBase.DrawWindow( )
Console.WriteLine("Writing string to the listbox: {0}", listBoxContents)
End Sub
Private listBoxContents As String " new member variable
End Class "ListBox
Public Class Button
Inherits Window
Public Sub New(ByVal top As Integer, ByVal left As Integer)
MyBase.New(top, left)
End Sub
Public Overrides Sub DrawWindow( )
Console.WriteLine( _
"Drawing a button at {0}, {1}" + ControlChars.Lf, top, Left)
End Sub "DrawWindow
End Class "Button
Override the Finalize Function
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim circle1, circle2 As Circle
circle1 = New Circle(72, 29, 4.5) " instantiate objects
circle2 = New Circle(5, 5, 10)
circle1 = Nothing " mark objects for garbage collection
circle2 = Nothing
System.GC.Collect() " request garbage collector to execute
End Sub
End Class
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
Console.WriteLine("Circle constructor: {0}", Me)
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
Console.WriteLine("Circle constructor: {0}", Me)
End Sub " New
" finalizer overrides version in class Point
Protected Overrides Sub Finalize()
Console.WriteLine("Circle Finalizer: {0}", Me)
MyBase.Finalize() " call Point finalizer
End Sub " Finalize
" property Radius
Public Property Radius() As Double
Get
Return mRadius
End Get
Set(ByVal radiusValue As Double)
If radiusValue > 0 Then
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 Overridable 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 reference to return Point String
Return "Center = " & MyBase.ToString() & _
"; Radius = " & mRadius
End Function " ToString
End Class
Public Class Point
" point coordinate
Private mX, mY As Integer
" default constructor
Public Sub New()
" implicit call to Object constructor occurs here
X = 0
Y = 0
Console.WriteLine("Point constructor: {0}", Me)
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
Console.WriteLine("Point constructor: {0}", Me)
End Sub " New
" finalizer overrides version in class Object
Protected Overrides Sub Finalize()
Console.WriteLine("Point Finalizer: {0}", Me)
MyBase.Finalize() " call Object finalizer
End Sub " Finalize
" 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
End Class