ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to demonstrate the simple inheritance with constructors

Here, we are going to demonstrate the simple inheritance with constructors in VB.Net.
Submitted by Nidhi, on December 26, 2020

Here, we will create a Sample1 class then create a new class Sample2 by extending the feature of Sample1 class using the Inherits keyword, Both classes contain constructors, here we understand the flow of constructor calling in inheritance.

Program/Source Code:

The source code to demonstrate the simple inheritance with constructors is given below. The given program is compiled and executed successfully.

'VB.net program to demonstrate the simple 
'inheritance with constructors.

Module Module1
    Class Sample1
        Sub New()
            Console.WriteLine("Sample1:Constructor called")
        End Sub
        Sub Fun1()
            Console.WriteLine("Sample1.Fun1() called")
        End Sub
    End Class
    Class Sample2
        Inherits Sample1
        Sub New()
            Console.WriteLine("Sample2:Constructor called")
        End Sub
        Sub Fun2()
            Console.WriteLine("Sample2.Fun2() called")
        End Sub
    End Class

    Sub Main()
        Dim obj As New Sample2()
        obj.Fun1()
        obj.Fun2()
    End Sub

End Module

Output:

Sample1:Constructor called
Sample2:Constructor called
Sample1.Fun1() called
Sample2.Fun2() called
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1. Here, we created two classes Sampl1 and Sample2, Here Sample1 is the parent class and Sample2 is the child class.

The Sample1 class contains a fun1() method and a constructor that will print text messages on the console screen. The Sample2 class inherits the feature of the Sample1 class, it also contains a fun2() method.

At last, we created a Main() function, which is the entry point for the program, here we created the object of Sample2 class, then it calls the constructor but here we inherited the Sample1 class into it. That's why it will constructor of the parent class first and then call the constructor of the child class. After that, we called the fun1() and fun2() method that will print an appropriate message on the console screen.

VB.Net Basic Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.