Inheritance

 Introduction to Inheritance

 Definition

Imagine you create a class used to process calculations for a circle. The class may appear as follows:

Imports System

Class Circle

Public Function CalculateDiameter() As Double
End Function

Public Function CalculateCircmuference() As Double
Return CalculateDiameter() * 3.14159
End Function

Public Function CalculateArea() As Double
End Function
End Class

Class Exercise
Public Shared Sub Main()
Dim circ As Circle = New Circle

Console.WriteLine(" -=- Circle Characteristics -=-")
Console.WriteLine("Diameter:      {0} ", circ.CalculateDiameter())
Console.WriteLine("Circmuference: {0} ", circ.CalculateCircmuference())
Console.WriteLine("Area:          {0} " & vbCrLf, circ.CalculateArea())
End Sub

End Class

This would produce:

-=- Circle Characteristics -=-
Diameter:         51.1
Circmuference: 160.535249
Area:                2050.837805975

After creating this class, imagine you want to create another class used to process a sphere. You may start thinking of creating a new class and redefining the members similar to those that belong to the above Circle class. To make this easier, you may just select the code of the Circle class, copy it, and then paste it in your document. Fortunately, when a class hold a foundation that another class can use, you can create the new class that is based on the old one: this is the foundation of class inheritance, or simply called inheritance.

Inheritance is the process of creating a new class that is based on an existing class.

 Class Derivation

As you may have guessed, in order to implement inheritance, you must first have a class that provides the fundamental definition or behavior you need. There is nothing magical about such a class. It could appear exactly like any of the classes we have used so far.

The above Circle class is used to process a circle. It can request or provide a radius. It can also calculate the circumference and the area of a circle. Since a sphere is primarily a 3-dimensional circle, and if you have a class for a circle already, you can simply create your sphere class that uses the already implemented behavior of a circle class.

Creating a class that is based on another class is also referred to as deriving a class from another. The first class serves as parent or base. The class that is based on another class is also referred to as child or derived. To create a class based on another, you use the following formula:

Class Child
Inherits Parent

' Body of the new class

End Class

In this formula, you start with the Class keyword, followed by a name for your new class, followed by an end of line. On the next line, type the Inherits keyword, followed by the name of the class on which the new Child class would be based. Of course, the Parent class must have been defined; that is, the compiler must be able to find its definition. Based on the above formula, you can create a sphere class based on the earlier mentioned Circle class as follows:

Imports System

Module Exercise

Class Circle

End Class

Class Sphere
Inherits Circle

End Class

End Module

As mentioned in the previous lesson, if you want to be able to access the class from other languages, you can precede its name with the Public keyword:

Imports System

Public Class Circle

End Class

Public Class Sphere
Inherits Circle

End Class

Class Exercise

Public Shared Sub main()

End Sub

End Class

Otherwise, if you intend to use it only in the current project, you can precede the Class of the class' name with the Private keyword.

 Characteristics of Inheritance

 Private Members