Inheritance and Polymorphism in VB.NET

In this article I will describe about the inheritance and polymorphism class's in VB.NET.
  • 2787

Inheritance and polymorphism are two key feature of OOP language, we discuss step by step about both of them.

Inheritance: Inheritance is the property in which, a derived class acquires the attributes of its base class. In simple terms, you can create or 'inherit' your own class (derived class), using an existing class (base class). You can use the Inherits keyword for this.

Polymorphism: Polymorphism is the property in which a single object can take more than one form. When you call a function in your object, the system will automatically determine the type of the object to call the appropriate function, if you are assigning an object of the base class to the base class variable, then the function in the base class will work.

EXAMPLE CODE

    Imports System.Collections
    Module Module1
        Sub Main()
            Dim Plot1 As New Triangular
            Dim Plot2 As New Circular
            Dim Plot3 As New Square
            Dim Shape As IEnumerator
            Dim dblTotalArea As Double

            Plot1.SideA = 4
            Plot1.SideB = 2.3
            Plot1.SideC = 1
            Console.WriteLine("Triangular plot area is " & Plot1.Area.ToString & ".")
            Plot2.Radius = 3

            Console.WriteLine("Circular plot area is " & Plot2.Area.ToString & ".")
            Plot3.Side = 10

            Console.WriteLine("Square plot area is " & Plot3.Area.ToString & ".")

            Dim plots() As Shape = {Plot1, Plot2, Plot3}

            Shape = plots.GetEnumerator
            While Shape.MoveNext
                dblTotalArea +=
CType(Shape.Current, Shape).Area
            End While
            Console.WriteLine("The total area is " & dblTotalArea.ToString & ".")
            Console.ReadLine()
        End Sub
    End
Module

    Public
MustInherit Class Shape
        Public MustOverride Function Area() As Double
        Public
MustOverride Function Perimeter() As Double
    End
Class

    Public
Class Square
        Inherits Shape
        Private mySide As Double|
        Public Property Side() As Double
            Get
                Side = mySide
            End Get
            Set
(ByVal Value As Double)
                mySide = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Area = mySide * mySide
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(4 * mySide)
    End Function

    End
Class

    Public
Class Triangular
        Inherits Shape
    Private mySide1, mySide2, mySide3 As Double
        Property
SideA() As Double
            Get
                SideA = mySide1
            End Get
            Set
(ByVal Value As Double)
                mySide1 = Value
            End Set
        End
Property

        Property
SideB() As Double
            Get
                SideB = mySide2
            End Get
            Set
(ByVal Value As Double)
                mySide2 = Value
            End Set
        End
Property
   
        Public
Property SideC() As Double
            Get
                SideC = mySide3
            End Get
            Set
(ByVal Value As Double)
                mySide3 = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Dim
Perim As Double
        Perim = Perimeter()
        Return (Math.Sqrt(Perim * (Perim - mySide1) * _
        (Perim - mySide2) * (Perim - mySide3)))
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(mySide1 + mySide2 + mySide3)
    End Function

    Sub
New(ByVal SideA As Double, ByVal SideB As Double, ByVal SideC As Double)
        MyBase.New()
        mySide1 = SideA
        mySide2 = SideB
        mySide3 = SideC
    End Sub

    Sub
New()
        MyBase.New()
    End Sub

    End
Class

    Public
Class Circular
        Inherits Shape
    Private Radius1 As Double
        Public
Property Radius() As Double
            Get
                Radius = Radius1
            End Get
            Set
(ByVal Value As Double)
                Radius1 = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Return
(Math.PI * Radius1 ^ 2)
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(2 * Math.PI * Radius1)
    End Function

    Sub
New()
        MyBase.New()
    End Sub

    Sub
New(ByVal Radius As Double)
        MyBase.New()
        Radius1 = Radius
    End Sub

    End
Class

OUTPUT


Inheritance and polymorphism are two key feature of OOP language, we discuss step by step about both of them.

Inheritance: Inheritance is the property in which, a derived class acquires the attributes of its base class. In simple terms, you can create or 'inherit' your own class (derived class), using an existing class (base class). You can use the Inherits keyword for this.

Polymorphism: Polymorphism is the property in which a single object can take more than one form. When you call a function in your object, the system will automatically determine the type of the object to call the appropriate function, if you are assigning an object of the base class to the base class variable, then the function in the base class will work.

EXAMPLE CODE

    Imports System.Collections
    Module Module1
        Sub Main()
            Dim Plot1 As New Triangular
            Dim Plot2 As New Circular
            Dim Plot3 As New Square
            Dim Shape As IEnumerator
            Dim dblTotalArea As Double

            Plot1.SideA = 4
            Plot1.SideB = 2.3
            Plot1.SideC = 1
            Console.WriteLine("Triangular plot area is " & Plot1.Area.ToString & ".")
            Plot2.Radius = 3

            Console.WriteLine("Circular plot area is " & Plot2.Area.ToString & ".")
            Plot3.Side = 10

            Console.WriteLine("Square plot area is " & Plot3.Area.ToString & ".")

            Dim plots() As Shape = {Plot1, Plot2, Plot3}

            Shape = plots.GetEnumerator
            While Shape.MoveNext
                dblTotalArea +=
CType(Shape.Current, Shape).Area
            End While
            Console.WriteLine("The total area is " & dblTotalArea.ToString & ".")
            Console.ReadLine()
        End Sub
    End
Module

    Public
MustInherit Class Shape
        Public MustOverride Function Area() As Double
        Public
MustOverride Function Perimeter() As Double
    End
Class

    Public
Class Square
        Inherits Shape
        Private mySide As Double|
        Public Property Side() As Double
            Get
                Side = mySide
            End Get
            Set
(ByVal Value As Double)
                mySide = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Area = mySide * mySide
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(4 * mySide)
    End Function

    End
Class

    Public
Class Triangular
        Inherits Shape
    Private mySide1, mySide2, mySide3 As Double
        Property
SideA() As Double
            Get
                SideA = mySide1
            End Get
            Set
(ByVal Value As Double)
                mySide1 = Value
            End Set
        End
Property

        Property
SideB() As Double
            Get
                SideB = mySide2
            End Get
            Set
(ByVal Value As Double)
                mySide2 = Value
            End Set
        End
Property
   
        Public
Property SideC() As Double
            Get
                SideC = mySide3
            End Get
            Set
(ByVal Value As Double)
                mySide3 = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Dim
Perim As Double
        Perim = Perimeter()
        Return (Math.Sqrt(Perim * (Perim - mySide1) * _
        (Perim - mySide2) * (Perim - mySide3)))
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(mySide1 + mySide2 + mySide3)
    End Function

    Sub
New(ByVal SideA As Double, ByVal SideB As Double, ByVal SideC As Double)
        MyBase.New()
        mySide1 = SideA
        mySide2 = SideB
        mySide3 = SideC
    End Sub

    Sub
New()
        MyBase.New()
    End Sub

    End
Class

    Public
Class Circular
        Inherits Shape
    Private Radius1 As Double
        Public
Property Radius() As Double
            Get
                Radius = Radius1
            End Get
            Set
(ByVal Value As Double)
                Radius1 = Value
            End Set
        End
Property

    Public
Overrides Function Area() As Double
        Return
(Math.PI * Radius1 ^ 2)
    End Function

    Public
Overrides Function Perimeter() As Double
        Return
(2 * Math.PI * Radius1)
    End Function

    Sub
New()
        MyBase.New()
    End Sub

    Sub
New(ByVal Radius As Double)
        MyBase.New()
        Radius1 = Radius
    End Sub

    End
Class

OUTPUT

8.gif
 

CONCLUSION

I hope my this contribution is helpful to you you can learn all the working area of polymorphism and inheritance by read this code.

 

CONCLUSION

I hope my this contribution is helpful to you you can learn all the working area of polymorphism and inheritance by read this code.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.