NB :
(I) To open a fresh Console Application or Module :- Open VB .net application first -File Menu – New – Project – Visual Basic – Console Application – ok.
(II) To open a fresh Window Form Application :- Open VB .net application first -File Menu – New – Project – Visual Basic – Window Forms Application – ok.
(III) To add a new module in a Project :-Open VB .net application first – Project Menu – Add Module – Module – Add.
(IV) To add a new Window Form in a Project :-Open the VB .net application first – Project Menu – Add Window Form – Window Form – Add.
————————————————– X ———————————————-
Example: An Inheritance Example in a VB .Net Program that displays output in different ways using Single Inheritance through a Module in the Console Application.
Imports System
Module Module1
Dim x, y, z As Integer
Class input
Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Class output
Inherits input
Sub result()
z = x + y
Console.WriteLine("Addition result are = " & z)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.read()
obj.result()
Console.ReadKey()
End Sub
End Module
---------------- X ----------------
Imports System
Module Module1
'Dim x, y, z As Integer
'Private x, y, z As Integer
'Public x, y, z As Integer
Class input
Protected x, y, z As Integer
Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Class output
Inherits input
Sub result()
z = x + y
Console.WriteLine("Addition result are = " & z)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.read()
obj.result()
Console.ReadKey()
End Sub
End Module
Output:
Enter two values
10
20
Addition result are = 30
---------------- X ----------------
Imports System
Module Module1
Public x, y, z As Integer
Public Class input
Public Sub read()
x = 10
y = 20
End Sub
End Class
Public Class output
Inherits input
Public Sub result()
z = x + y
Console.WriteLine("Addition result are = " & z)
End Sub
End Class
Sub Main()
Dim obj As New output 'object creation'
'Dim obj As output = New output' 'object creation'
obj.read()
obj.result()
Console.ReadKey()
End Sub
End Module
Output:
Addition result are = 30
---------------------- X ----------------------
Module Module1
Dim x, y, z As Integer
Public Class input
Public Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Public Class output
Inherits input
Public Sub result()
z = x + y
Console.WriteLine("Addition result are = " & z)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.read()
obj.result()
Console.ReadKey()
End Sub
End Module
Output :
Enter two values
20
50
Addition result are = 70
---------------------- X ----------------------
Module Module1
Dim x, y, z, m, n As Integer
Public Class input
Public Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Public Class output
Inherits input
Public Sub result()
z = x + y + m + n
Console.WriteLine("Addition result are = " & z)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.read()
m = 50
n = 30
obj.result()
Console.ReadKey()
End Sub
End Module
Output :
Enter two values
10
10
Addition result are = 100
---------------------- X ----------------------
Module Module1
Dim x As Integer
Dim y As String
Public Class input
Public Sub read(ByVal i As Integer, ByVal n As String) 'Parameterised subroutine'
x = i
y = n
End Sub
End Class
Public Class output : Inherits input
Public Sub result()
Console.WriteLine("The entered values are = " & x & " " & y)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.read(10, "Welcome")
obj.result()
Console.ReadKey()
End Sub
End Module
Output :
The entered values are = 10 Welcome
---------------------- X ----------------------
Module Module1
Dim x As Integer
Dim y As String
Public Class input
Public Sub New() 'constructor creation'
x = 10
y = "Welcome"
End Sub
End Class
Public Class output : Inherits input
Public Sub result()
Console.WriteLine("The entered values are = " & x & " " & y)
End Sub
End Class
Sub Main()
Dim obj As New output
obj.result()
Console.ReadKey()
End Sub
End Module
Output :
The entered values are = 10 Welcome
Example : An Inheritance Examples in VB .Net Program to display Inheritance through a Protected Access Modifier using a Module in the Console Application.
Module Module1
Class Program1
Protected Sub Output()
Console.WriteLine("Program1 Function Executed")
End Sub
End Class
Class Program2
Inherits Program1
Sub Display()
Output()
Console.WriteLine("Program2 Function Executed")
End Sub
End Class
Sub Main()
Dim obj As New Program2()
'obj.Output() 'Not Accessible directly due to protected
obj.Display()
End Sub
End Module
Output:
Program1 Function Executed
Program2 Function Executed
Example : A VB .Net Program to display output differently using Hierarchical Inheritance through a Module in the Console Application.
Imports System
Module Module1
Dim x, y, z, m, n As Integer
Class A
Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Class A1
Inherits A
Sub result1()
z = x + y
Console.WriteLine("Addition result is = " & z)
End Sub
End Class
Class A2
Inherits A
Sub result2()
m = x - y
Console.WriteLine("Subtraction result is = " & m)
End Sub
End Class
Class A3
Inherits A
Sub result3()
n = x * y
Console.WriteLine("Multiplication result is = " & n)
End Sub
End Class
Sub Main()
Dim obj1 As New A1
obj1.read()
obj1.result1()
Dim obj2 As New A2
obj2.read()
obj2.result2()
Dim obj3 As New A3
obj3.read()
obj3.result3()
Console.ReadKey()
End Sub
End Module
Output:
Enter two values
10
20
Addition result is = 30
Enter two values
30
20
Subtraction result is = 10
Enter two values
40
50
Multiplication result is = 2000
Example : A VB .Net Program to display output differently using MultiLevel Inheritance through Module in Console Application.
Imports System
Module Module1
Dim x, y, z, m, n As Integer
Class A
Sub read()
Console.WriteLine("Enter two values")
x = Console.ReadLine()
y = Console.ReadLine()
End Sub
End Class
Class A1
Inherits A
Sub result1()
z = x + y
Console.WriteLine("Addition result is = " & z)
End Sub
End Class
Class A2
Inherits A1
Sub result2()
m = x - y
Console.WriteLine("Subtraction result is = " & m)
Console.WriteLine("Its Base Class result was = " & z)
End Sub
End Class
Class A3
Inherits A2
Sub result3()
n = x * y
Console.WriteLine("Multiplication result is = " & n)
Console.WriteLine("Its Base Class result was = " & m)
Console.WriteLine("The Super Base Class result was = " & z)
End Sub
End Class
Sub Main()
Dim obj1 As New A1
obj1.read()
obj1.result1()
Dim obj2 As New A2
obj2.read()
obj2.result2()
Dim obj3 As New A3
obj3.read()
obj3.result3()
Console.ReadKey()
End Sub
End Module
Output:
Enter two values
10
20
Addition result is = 30
Enter two values
40
30
Subtraction result is = 10
Its Base Class result was = 30
Enter two values
10
10
Multiplication result is = 100
Its Base Class result was = 10
The Super Base Class result was = 30
Example : Write a VB.Net Program to call the Base and Derived class Overridable methods (Method Overriding).
Module Module1
Class A
Overridable Sub Display()
Console.WriteLine("Hello World")
End Sub
End Class
Class B
Inherits A
Overrides Sub Display()
Console.WriteLine("Hello India")
End Sub
End Class
Sub Main()
Dim Obj1 As A = New A()
Obj1.Display()
Dim Obj2 As B = New B()
Obj2.Display()
Dim Obj3 As A = New B()
Obj3.Display()
Obj1 = New B()
Obj1.Display()
End Sub
End Module
Output:
Hello World
Hello India
Hello India
Hello India
Example : Write a VB.Net Program to Override the Base class method into a Derived class (Simple Method Overriding).
Module Module1
Class A
Overridable Sub DisplayMsg()
Console.WriteLine("Hello World")
End Sub
End Class
Class B
Inherits A
Overrides Sub DisplayMsg()
Console.WriteLine("Hello India")
End Sub
End Class
Sub Main()
Dim Obj1 As New A()
Obj1.DisplayMsg()
Dim Obj2 As New B()
Obj2.DisplayMsg()
End Sub
End Module
Output:
Hello World
Hello India
Example : Write a VB.Net Program to demonstrate how to Stop the Method Overriding using NotInheritable Class.
Module Module1
NotInheritable Class A
Sub Display()
Console.WriteLine("Hello World")
End Sub
End Class
Class B
Inherits A
Sub Display()
Console.WriteLine("Hello India")
End Sub
End Class
Sub Main()
Dim Obj1 As New A()
Dim Obj2 As New B()
Obj1.Display()
Obj2.Display()
End Sub
End Module
Output:
Error BC30299 'B' cannot inherit from class 'A' because 'A' is declared 'NotInheritable'.
Example : Write a VB.Net Program to demonstrate the ‘MyBase’ Keyword (Like Super Keyword).
Module Module1
Class Program1
Sub Output()
Console.WriteLine("Hello World")
End Sub
End Class
Class Program2
Inherits Program1
Sub Output()
MyBase.Output()
Console.WriteLine("Hello India")
End Sub
End Class
Sub Main()
Dim Obj As New Program2()
Obj.Output()
End Sub
End Module
Output:
Hello World
Hello India
0 Comments