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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Class Inheritance 
6.14.9.Class inheritance and polymorphism
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 (* 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 (* 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.