=================================================================
Inherits Keyword
=================================================================
Example #1
Module Module11
Sub Main()
Dim c As Child
c = New Child
c.P1()
c.C1()
Console.ReadLine ()
End Sub
End Module
Public Class Parent
Public Sub P1()
Console.WriteLine ("Parent P1")
End Sub
End Class
Public Class Child: Inherits Parent
Public Sub C1()
Console.WriteLine ("Child C1")
End Sub
End Class
Output #1
Parent P1
Child C1
=================================================================
Overriding Methods
=================================================================
Example #2
Module Module12
Sub Main()
Dim obj As New SubClass
obj.DoSomething()
Console.ReadLine()
End Sub
End Module
Public Class Parent
Public Overridable Sub DoSomething()
Console.WriteLine("Hello from Parent")
End Sub
End Class
Public Class SubClass
Inherits Parent
Public Overrides Sub DoSomething()
Console.WriteLine("Hello from SubClass")
End Sub
End Class
Output #2
Hello from SubClass
=================================================================
Function Overloading
=================================================================
Example #3
Module Module3
Public Sub Main()
Dim counter As Calculator
counter = New Calculator
' pass two integers
Console.WriteLine(counter.Add(1, 5))
' pass two doubles
Console.WriteLine(counter.Add(1.3, 5.9))
Console.ReadLine()
End Sub
End Module
Class Calculator
Public Overloads Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
Add = a + b
End Function
Public Overloads Function Add(ByVal a As Double, ByVal b As Double) As Double
Add = a + b
End Function
End Class
Output #3
6
7.2
=================================================================
Method Overloading
=================================================================
Example #4
Module Module4
Sub Main()
Dim mc1 As New MyClass1
Dim mc2 As New MyClass2
mc1.Method1()
mc2.Method1()
mc2.Method2(10)
mc2.Method2("Saravanan")
Console.ReadLine()
End Sub 'Main
End Module
Class MyClass1
Public Overridable Sub Method1()
Console.WriteLine("Method #1")
End Sub
End Class 'MyClass1
Class MyClass2
Inherits MyClass1
Public Overloads Sub Method1()
Console.WriteLine("Overloading No Argument")
End Sub
Public Overloads Sub Method2(ByVal i As Integer)
Console.WriteLine("Overloading Integer Argument")
End Sub
Public Overloads Sub Method2(ByVal s As String)
Console.WriteLine("Overloading String Argument")
End Sub
End Class 'MyClass2
Output #4
Method #1
Overloading No Argument
Overloading Integer Argument
Overloading String Argument
=================================================================
Overriding Methods
=================================================================
Example #5
Module Module5
Sub Main()
Dim employee As Employee
employee = New Manager
employee.Work()
employee = New Employee
employee.Work()
Dim manage As Manager
manage = New Manager
manage.Manage()
manage.Work()
Console.ReadLine()
End Sub
End Module
Class Employee
Public Overridable Sub Work()
System.Console.WriteLine("I am an employee.")
End Sub
End Class
Class Manager : Inherits Employee
Public Sub Manage()
System.Console.WriteLine("Managing ...")
End Sub
Public Overrides Sub Work()
System.Console.WriteLine("I am a manager")
End Sub
End Class
Output #5
I am a manager
I am an employee.
Managing ...
I am a manager
=================================================================
MustInherit & MustOverride (Abstract Class & Abstract Method)
=================================================================
Example #6
Module Module6
Sub Main()
Dim mc As New MyClass2
mc.AbMethod1()
mc.AbMethod2()
Console.ReadLine()
End Sub 'Main
End Module
MustInherit Class MyAbs
Public MustOverride Sub AbMethod1()
Public MustOverride Sub AbMethod2()
End Class 'MyAbs
'not necessary to implement all abstract methods partial implementation is possible
MustInherit Class MyClass1
Inherits MyAbs
Public Overrides Sub AbMethod1()
Console.WriteLine("Abstarct method #1")
End Sub 'AbMethod1
End Class 'MyClass1
Class MyClass2
Inherits MyClass1
Public Overrides Sub AbMethod2()
Console.WriteLine("Abstarct method #2")
End Sub 'AbMethod2
End Class 'MyClass2
Output #6
Abstarct method #1
Abstarct method #2
=================================================================
MustInherit & MustOverride (Abstract Class & Abstract Method)
=================================================================
Example #7
Module Module7
Sub Main()
Dim mc As New MyClass1
mc.AbMethod1()
mc.AbMethod2()
Console.ReadLine()
End Sub 'Main
End Module
MustInherit Class MyAbs
Public MustOverride Sub AbMethod1()
Public MustOverride Sub AbMethod2()
End Class 'MyAbs
Class MyClass1
Inherits MyAbs
Public Overrides Sub AbMethod1()
Console.WriteLine("Abstarct method #1 of MyClass1")
End Sub 'AbMethod1
Public Overrides Sub AbMethod2()
Console.WriteLine("Abstarct method #2 of MyClass1")
End Sub 'AbMethod2
End Class 'MyClass1
Output #7
Abstarct method #1 of MyClass1
Abstarct method #2 of MyClass1
=================================================================
MustInherit & NotInheritable
=================================================================
Example #8
Module Module8
Sub Main()
Dim a As New A
a.AbMethod()
a.AA()
a.NonAbMethod()
Dim c As New C
c.CC()
Dim d As New D
d.AbMethod()
d.NonAbMethod()
d.AA()
Console.ReadLine()
End Sub
End Module
Public Class A
Inherits B
Public Sub New()
Console.WriteLine("Class A Constructor!")
End Sub
'Overriding refers to the methods in the child class having the same signature (name as well as ‘parameters) as of the parent class methods.
Public Overrides Sub AbMethod()
Console.WriteLine("Abstarct Method!")
End Sub
Public Overridable Sub AA()
Console.WriteLine("Class A Overridable AA!")
End Sub
End Class
Public MustInherit Class B
'Note : MustInherit means that the class cannot be instantiated
'(DerivedObjAs New BaseObj) but rather must be inherited.
Public Sub New()
Console.WriteLine("Abstract Class!")
End Sub
Public MustOverride Sub AbMethod() ' An abstract method
Public Sub NonAbMethod() ' NonAbMethod
Console.WriteLine("Non-Abstract Method")
End Sub
End Class
Public NotInheritable Class C
'Note : NotInheritable means that the class cannot be inherited
'(DerivedObjInherits BaseObj) but rather must be instantiated.
Public Sub New()
Console.WriteLine("Class C Constructor!")
End Sub
Public Sub CC()
Console.WriteLine("Class C Method CC")
End Sub
End Class
Public Class D
Inherits A
Public Overrides Sub AA()
Console.WriteLine("Overriding AA!")
End Sub
End Class
Output #8
Abstract Class!
Class A Constructor!
Abstract Method!
Class A Overridable AA!
Non-Abstract Method
Class C Constructor!
Class C Method CC
Abstract Class!
Class A Constructor!
Abstract Method!
Non-Abstract Method
Overriding AA!
=================================================================
Implements & Interface & Multiple Inheritance
=================================================================
Example #9
Module Module9
Sub Main()
Dim obj As New Calculator
Console.WriteLine("Add (567+234) :" & obj.Add(567, 234))
Console.WriteLine("Substract(567-234) :" & obj.Subtract(567, 234))
Console.ReadLine()
End Sub 'Main
End Module
Public Interface IAdd
Function Execute(ByVal i As Integer, ByVal j As Integer) As Integer
End Interface
Public Interface ISubtract
Function Execute(ByVal i As Integer, ByVal j As Integer) As Integer
End Interface
Public Class Calculator
Implements IAdd, ISubtract
Public Function Add(ByVal i As Integer, ByVal j As Integer) As Integer Implements IAdd.Execute
Return i + j
End Function
Public Function Subtract(ByVal i As Integer, ByVal j As Integer) As Integer Implements ISubtract.Execute
Return i - j
End Function
End Class
Output #9
Add (567+234) : 801
Substract(567-234) : 333
=================================================================
Shadows
=================================================================
Example #10
Module Module10
Sub Main()
Dim mc As New MyClass1
mc.Method()
Console.ReadLine()
End Sub 'Main
End Module
Public Class MyAbs
Public Sub Method()
Console.WriteLine("Method")
End Sub
End Class 'MyAbs
Class MyClass1
Inherits MyAbs
Public Shadows Sub Method()
Console.WriteLine("Shadow Method")
End Sub
End Class 'MyClass1
Output #10
Shadow Method
=================================================================
Shadowing Classes & Shadowing nested classes
=================================================================
Example #11
Module Module11
Sub Main()
Dim mc1 As New MyClass1
mc1.Method()
Console.ReadLine()
End Sub 'Main
End Module
Public Class MyAbs
Protected Class MyClass2
Public Shared Name As String = "MyAbs - NestedClass"
End Class
Public Sub Method()
Console.WriteLine(MyClass2.Name)
End Sub
End Class 'MyAbs
Public Class MyClass1
Inherits MyAbs
Protected Shadows Class MyClass2
Public Shared Name As String = "MyClass1 - NestedClass"
End Class
Public Shadows Sub Method()
Console.WriteLine(MyClass2.Name)
End Sub
End Class 'MyClass1
Output #11
MyClass1 - NestedClass
=================================================================
MyBase & MyClass Keywords
=================================================================
Example #12
Module Module12
Sub Main()
Dim p As Parent
Dim c As Child
p = New Parent
p.P1()
p.P2()
p = New Child
p.P1()
Console.ReadLine()
End Sub
End Module
Public Class Parent
Public Overridable Sub P1()
Console.WriteLine("Parent P1")
End Sub
Public Sub P2()
MyClass.P1()
'Implementing keyword MyClass here tells all derived classes to refer to the
'base / abstract class wherein the keyword MyClass appears
End Sub
End Class
Public Class Child : Inherits Parent
Public Overrides Sub P1()
Console.WriteLine("Child P1")
MyBase.P2()
End Sub
End Class
Output #12
Parent P1
Parent P1
Child P1
Parent P1
=================================================================
Me Keyword
=================================================================
Example #13
Module Module13
Sub Main()
Dim ball As Sphere = New Sphere
ball.ShowCharacteristics()
Console.ReadLine()
End Sub
End Module
Public Class Circle
Public Radius As Double
Public Function CalculateDiameter() As Double
Return Radius * 2
End Function
Public Function CalculateCircumference() As Double
Return CalculateDiameter() * 3.14159
End Function
End Class
Public Class Sphere
Inherits Circle
Public Sub ShowCharacteristics()
Me.Radius = 35.84
Console.WriteLine("Circle Characteristics")
Console.WriteLine("===================================")
Console.WriteLine("Radius: {0}", Me.Radius)
Console.WriteLine("Diameter: {0}", Me.CalculateDiameter())
Console.WriteLine("Circumference: {0}", Me.CalculateCircumference())
Console.WriteLine("===================================")
End Sub
End Class
Output #13
Circle Characteristics
===================================
Radius: 35.84
Diameter: 71.68
Circumference:225.1891712
===================================
Click Here for About Inheritance[FAQ]...