Class inheritance and polymorphism : Class Inheritance « Class Module « VB.Net Tutorial






Imports System.Collections

public class Test
   public Shared Sub Main
        Dim objShape1 As New Triangle
        Dim objShape2 As New Circle
        Dim objShape3 As New Square
        Dim ShapeEnum As IEnumerator
        Dim dblTotalArea As Double

        objShape1.SideA = 3
        objShape1.SideB = 3.2
        objShape1.SideC = 0.94
        Console.WriteLine("The triangle's area is " & objShape1.Area.ToString & ".")

        objShape2.Radius = 4
        Console.WriteLine("The circle's area is " & objShape2.Area.ToString & ".")

        objShape3.Side = 10.01
        Console.WriteLine("The square's area is " & objShape3.Area.ToString & ".")

        Dim aryShapes() As Shape = {objShape1, objShape2, objShape3}

        ShapeEnum = aryShapes.GetEnumerator
        While ShapeEnum.MoveNext
            dblTotalArea += CType(ShapeEnum.Current, Shape).Area
        End While
        Console.WriteLine("The total area of your shapes is " & dblTotalArea.ToString & ".")

   End Sub
End class

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 Triangle
    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 dblPerim As Double
        dblPerim = Perimeter()
        Return (Math.Sqrt(dblPerim * (dblPerim - mySide1) * _
        (dblPerim - mySide2) * (dblPerim - 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 Circle
    Inherits Shape
    Private m_dblRadius As Double
    Public Property Radius() As Double
        Get
            Radius = m_dblRadius
        End Get
        Set(ByVal Value As Double)
            m_dblRadius = Value
        End Set
    End Property
    Public Overrides Function Area() As Double
        Return (Math.PI * m_dblRadius ^ 2)
    End Function
    Public Overrides Function Perimeter() As Double
        Return (2 * Math.PI * m_dblRadius)
    End Function
    Sub New()
        MyBase.New()
    End Sub
    Sub New(ByVal Radius As Double)
        MyBase.New()
        m_dblRadius = Radius
    End Sub
End Class
The triangle's area is 26.8715818068085.
The circle's area is 50.2654824574367.
The square's area is 100.2001.
The total area of your shapes is 177.337164264245.








6.14.Class Inheritance
6.14.1.Simple Inheritance
6.14.2.Class implicitly Inherits Object
6.14.3.Circle class that inherits from class Point
6.14.4.Inherit ToString method from Object
6.14.5.Call base method
6.14.6.Shadows member function from base class
6.14.7.Use For Each for Class Hierarchy
6.14.8.Inherit constant from base class
6.14.9.Class inheritance and polymorphism
6.14.10.Multilevel inheritance