Inheritance in VB.NET

This article shows Inheritance in VB.NET.
  • 2361

In this article You will learn how to Create Inheritance in VB.NET.

Inheritance: Some important point related to the inheritance.

  1. Inheritance Provides re-usability of code.

  2. Helps in reducing the code size and better project management.

  3. Use Inherits keyword to inherit a class into other class.

  4. The old class is called the Base class and the new class is called Derived class.

  5. Use mybase keyword to reference the members of base class.

The general form of deriving a new class from an existing class looks as follows:

 

Public Class m1
---
---
End Class

Public Class m2
Inherits m1
---
---
End Class

 

Code for creating Inheritance:

The following code creates Inheritance.

 

Namespace inheritance

    Class Num2

        Private a As Integer, b As Integer

        Public Sub New(ByVal a As Integer, ByVal b As Integer)

            Me.a = a

            Me.b = b

        End Sub

        Public Function G2() As Integer

            Return If(a > b, a, b)

        End Function

        Public Function P2() As Integer

            Return a * b

        End Function

    End Class

    Class Num3

        Inherits Num2

        Private c As Integer

        Public Sub New(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)

            MyBase.New(a, b)

            Me.c = c

        End Sub

        Public Function G3() As Integer

            Return If(G2() > c, G2(), c)

        End Function

        Public Function P3() As Integer

            Return P2() * c

        End Function

 

    End Class

    Class InhTest

        Public Shared Sub Main()

            Dim x As New Num3(4, 5, 6)

            Console.WriteLine(x.P3())

            Console.WriteLine(x.P2())

        End Sub

    End Class

End Namespace

 

Output for the above code:


 inh.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.