Four Level Hierachy : Inheritance « Class « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Class » InheritanceScreenshots 
Four Level Hierachy
Four Level Hierachy

Imports System

Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
      Dim point As New Point(711)
      Dim circle As New Circle(2283.5)
      Dim cylinder As New Cylinder(10103.310)

      Dim arrayOfShapes As IShape() = New IShape(2) {}

      arrayOfShapes(0= point

      arrayOfShapes(1= circle

      arrayOfShapes(2= cylinder

      Console.WriteLinepoint.Name & ": " & _
         point.ToString() & vbCrLf & circle.Name & ": " & _
         circle.ToString() & vbCrLf & cylinder.Name & _
         ": " & cylinder.ToString() )

      Dim shape As IShape

      For Each shape In arrayOfShapes
         Console.WriteLine(shape.Name & ": " "Area = " & _
            String.Format("{0:F}", shape.Area()) & _
            "Volume = " & String.Format("{0:F}", shape.Volume()) )
      Next

    End Sub
End Class

Public Interface IShape
   Function Area() As Double
   Function Volume() As Double
   ReadOnly Property Name() As String

End Interface

Public Class Point
   Implements IShape

   Private mX, mY As Integer

   Public Sub New()
      X = 0
      Y = 0
   End Sub

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer)
      X = xValue
      Y = yValue
   End Sub

   Public Property X() As Integer
      Get
         Return mX
      End Get

      Set(ByVal xValue As Integer)
         mX = xValue
      End Set

   End Property

   Public Property Y() As Integer
      Get
         Return mY
      End Get

      Set(ByVal yValue As Integer)
         mY = yValue
      End Set

   End Property

   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function ' ToString

   Public Overridable Function Area() As Double _
      Implements IShape.Area

      Return 0
   End Function
   
   Public Overridable Function Volume() As Double _
      Implements IShape.Volume
   Return 0
   End Function

   Public Overridable ReadOnly Property Name() As String _
      Implements IShape.Name
      Get
         Return "Point"
      End Get
   End Property

End Class 


Public Class Circle
   Inherits Point

   Private mRadius As Double

   Public Sub New()
      Radius = 0
   End Sub

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double)

      MyBase.New(xValue, yValue)
      Radius = radiusValue
   End Sub

   Public Property Radius() As Double
      Get
         Return mRadius
      End Get

      Set(ByVal radiusValue As Double)
         If radiusValue >= Then
            mRadius = radiusValue
         End If
      End Set

   End Property

   Public Function Diameter() As Double
      Return mRadius * 2
   End Function

   Public Function Circumference() As Double
      Return Math.PI * Diameter()
   End Function

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

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Circle"
      End Get

   End Property

   Public Overrides Function ToString() As String
      Return "Center = " & MyBase.ToString() & _
         "; Radius = " & mRadius
   End Function ' ToString
End Class


Public Class Cylinder
   Inherits Circle 
   
   Private mHeight As Double

   Public Sub New()
      Height = 0
   End Sub

   Public Sub New(ByVal xValue As Integer, _
      ByVal yValue As Integer, ByVal radiusValue As Double, _
      ByVal heightValue As Double)

      MyBase.New(xValue, yValue, radiusValue)
      Height = heightValue
   End Sub

   Public Property Height() As Double
      Get
         Return mHeight
      End Get

      Set(ByVal heightValue As Double)
         If heightValue >= Then
            mHeight = heightValue
         End If

      End Set

   End Property

   Public Overrides Function Area() As Double
      Return * MyBase.Area + MyBase.Circumference * mHeight
   End Function

   Public Overrides Function Volume() As Double
      Return MyBase.Area * mHeight
   End Function 

   Public Overrides Function ToString() As String
      Return MyBase.ToString() "; Height = " & mHeight
   End Function ' ToString

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Cylinder"
      End Get

   End Property 
End Class

           
       
Related examples in the same category
1.Construct Class by Class CombinationConstruct Class by Class Combination
2.Property Shadow during InheritanceProperty Shadow during Inheritance
3.Class Inheritance DemoClass Inheritance Demo
w_w___w_.ja_v___a2__s.___com___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.