Page 1 of 1

## Inheritance Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=268601&amp;s=0728213745a9de0d014f8d424b7d7003&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

Posted 27 February 2012 - 08:55 PM

Inheritance

Let's make a simple example for a Coordinate

The Base-Class

You can the that this must be a base-class by the presence of the MustInherit.

```Public MustInherit Class Coordinate
Protected Friend Sub New()
End Sub
Public MustOverride Overrides Function ToString() As String

End Class

```

1D_X

1D_X <: Coordinate

This is a sub-class of the Coordinate base-class.

```Public Class 1D_X
Inherits Coordinate
Private _X As Integer = 0

Public ReadOnly Property X As Integer
Get
Return _X
End Get
End Property
Public Sub New(X As Integer)
MyBase.New()
_X = X
End Sub
Public Overrides Function ToString() As String
Return String.Format("X:= {0}", Me.X)
End Function
End Class
```

Spoiler

2D_XY

2D_XY <: 1D_XY <: Coordinate

So this inherits from the features of the base-class 1D_XY which in turn inherits from the base-class Coordinate.

```Public Class XY
Inherits 1D_X
Private _Y As Integer = 0
Public ReadOnly Property Y As Integer
Get
Return _Y
End Get
End Property
Public Sub New(X As Integer, Y As Integer)
MyBase.New(X) ' Call the Constructor of the base-class I'm inheriting from.
_Y = Y
End Sub
Public Overrides Function ToString() As String
Return MyBase.ToString() + " " + String.Format("Y:= {0}", Me.Y)
End Function
End Class
```

3D_XYZ

You'll notice that this example has the keyword NotInheritable modifier on the class name.
This means that the class can't be used as a base-class.
```Public NotInheritable Class 3D_XYZ
Inherits 2D_XY
Private _Z As Integer = 0
Public Sub New(X As Integer, Y As Integer, Z As Integer)
MyBase.New(X, Y) ' Call the Constructor of the base-class I'm inheriting from.
End Sub
Public ReadOnly Property Z As Integer
Get
Return _Z
End Get
End Property
Public Overrides Function ToString() As String
Return MyBase.ToString() + " " + String.Format("Z:={0}", Me.Z)
End Function
End Class

```

Polar
```Public Class Polar
Inherits Coordinate

Public Sub New(Angle As Double, Distance As Double)
MyBase.New()
_Angle = Angle : _Distance = Distance
End Sub
Private _Angle As Double
Public Property Angle() As Double
Get
Return _Angle
End Get
Set(ByVal value As Double)
_Angle = value
End Set
End Property
Private _Distance As Double
Public Property Distance() As Double
Get
Return _Distance
End Get
Set(ByVal value As Double)
_Distance = value
End Set
End Property

Public Overrides Function ToString() As String
Return String.Format("Angle:={0} Distance:={1}", Me.Angle, Me.Distance)
End Function
End Class

```

Let's add another function to the base-class coordinate.

```  Public Overridable Function What() As String
Return "Some Default Message"
End Function

```

You'll notice it has the modifier Overridable this means that it can be overriden by the sub-class, if you don't provide one the one provided by the base-class is used.

To see the effect of this add this to 2D_XY
```  Public Overrides Function What() As String
Return "Overridden What Method"
End Function

```

Sample Usage

A little Console Application demo.
```Module Module1

Sub Main()
Dim x As New X(1)
Dim xy As New XY(2, 3)
Dim xyz As New XYZ(4, 5, 6)
Dim p As New Polar(90, 10)
Dim coords() As Coordinate = {x, xy, xyz, p}
For Each c In coords
Console.WriteLine(c.ToString())
Console.WriteLine(c.What())
Console.WriteLine()
Next
End Sub

End Module
```

Outputs
```X:= 1
Some Default Message

X:= 2 Y:= 3
Overridden What Method

X:= 4 Y:= 5 Z:=0
Overridden What Method

Angle:=90 Distance:=10
Some Default Message

```

You'll notice the two Overridden What Method when we only wrote one in the code.

Where did it come from?

Spoiler

Limitations

• Single Inheritance
You can only inherit from one base-class.
• Non-Inherited Constructors
The Constructor(s) of the base-class are not inherited by the sub-class, thus you to implement them and call the base-class's constructor in the sub-class's constructor.

So Thank-You reading this tutorial on Inheritance.

Is This A Good Question/Topic? 2

## Replies To: Inheritance

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

Posted 28 February 2012 - 12:00 PM

This tutorial has a small bug in it, which is a good excuse to utilise our debugging skills.

Inheritance Tutorial Debugged

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}