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 Examples in VB .Net Program to display 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 in different ways 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 in different ways 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 class and Derived class Overridable method (Method Overriding).
Module Module1
    Class Example1
        Overridable Sub Display()
            Console.WriteLine("Example One Display Function Executed")
        End Sub
    End Class

    Class Example2
        Inherits Example1
        Overrides Sub Display()
            Console.WriteLine("Example Two Display Function Executed")
        End Sub
    End Class

    Sub Main()
        Dim Obj1 As Example1 = New Example1()
        Obj1.Display()
        Obj1 = New Example2()
        Obj1.Display()
    End Sub
End Module

Output:
Example One Display Function Executed
Example Two Display Function Executed
Example : Write a VB.Net Program to Override the Base class method into a Derived class (Method Overriding).
Module Module1
    Class Example1
        Overridable Sub DisplayMsg()
            Console.WriteLine("Example One Display Message Executed")
        End Sub
    End Class

    Class Example2
        Inherits Example1
        Overrides Sub DisplayMsg()
            Console.WriteLine("Example Two Display Message Executed")
        End Sub
    End Class

    Sub Main()
        Dim Obj As New Example2()
        Obj.DisplayMsg()
    End Sub
End Module

Output:
Example Two Display Message Executed
Example : Write a VB.Net Program to demonstrate the NotInheritable Class.
Module Module1
    NotInheritable Class A
        Sub Display()
            Console.WriteLine("Display Function is Called")
        End Sub
    End Class

    Class B
        Inherits A
        Sub Print()
            Console.WriteLine("Print Function is Called")
        End Sub

    End Class

    Sub Main()
        Dim Obj1 As New A()
        Dim Obj2 As New B()

        Obj1.Display()
        Obj2.Print()
    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 Output1()
            Console.WriteLine("Program1 Method Executed")
        End Sub
    End Class
    Class Program2
        Inherits Program1
        Sub Output2()
            MyBase.Output1()

            Console.WriteLine("Program2 Method Executed")
        End Sub
    End Class
    Sub Main()
        Dim Obj As New Program2()
        Obj.Output2()
    End Sub
End Module

Output:
Program1 Method Executed
Program2 Method Executed

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.