Home

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 Radius As Double

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

    Public Function CalculateArea() As Double
        Return Radius * Radius * 3.14159
    End Function
End Class

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

        Console.WriteLine(" -=- Circle Characteristics -=-")
        Console.WriteLine("Radius:        {0} ", circ.Radius)
        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 -=-
Radius:             25.55
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

After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example:

Imports System

Public Class Circle

End Class

Public Class Sphere
    Inherits Circle

End Class

Class Exercise

    Public Shared Sub main()

        Dim ball As Sphere = New Sphere

    End Sub

End Class

When a class is based on another class, all public members of the parent class are made available to the derived class that can use them as necessary. While other methods and classes can also use the public members of a class, the difference is that the derived class can call the public members of the parent as if they belonged to the derived class. That is, the child class doesn't have to "qualify" the public members of the parent class when these public members are used in the body of the derived class. This is illustrated in the following program:

Imports System

Public Class Circle

    Public Radius As Double

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

Public Class Sphere
    Inherits Circle

    Public Sub ShowCharacteristics()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Radius = 35.84

        Console.WriteLine("Circle Characteristics")
        Console.WriteLine("=======================================================")
        Console.WriteLine("Radius:             {0}", Radius)
        Console.WriteLine("Diameter:         {0}", CalculateDiameter())
        Console.WriteLine("Circumference: {0}", CalculateCircumference())
        Console.WriteLine("=======================================================")
    End Sub

End Class

Class Exercise

    Public Shared Sub main()

        Dim ball As Sphere = New Sphere

        ball.ShowCharacteristics()

    End Sub

End Class

This would produce:

Sphere Characteristics
Radius:        35.84
Diameter:      71.68
Circumference: 225.1891712

Based on the relationship between a child class and its parent, you can use Me in the child to access the public members of the parent class. Here is an example:

Imports System

Public Class Circle

    Public Radius As Double

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

Public Class Sphere
    Inherits Circle

    Public Sub ShowCharacteristics()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 35.84

        Console.WriteLine("Circle Characteristics")
        Console.WriteLine("=======================================================")
        Console.WriteLine("Radius:             {0}", Me.Radius)
        Console.WriteLine("Diameter:         {0}", Me.CalculateDiameter())
        Console.WriteLine("Circumference: {0}", Me.CalculateCircumference())
        Console.WriteLine("=======================================================")
    End Sub

End Class

Class Exercise

    Public Shared Sub main()

        Dim ball As Sphere = New Sphere

        ball.ShowCharacteristics()

    End Sub

End Class

We mentioned that, when deriving a new class based on an existing one, all public members of the parent class are accessible to its child classes. In fact, those public members are also available to all other non-child classes and external procedures of the parent class. On the other hand, when creating a class, if you think that a certain member would not need to be accessed outside of the class, you should make it private by starting it with the Private keyword. Here is an example:

Imports System

Public Class Circle

    Public Radius As Double

    Private Function ShowDescription()
        Return "A circle is a round geometric shape constructed " & _
               "so that all considered points of the shape are " & _
               "at an equal distance from a common point called " & _
               "the center. Also, two equally opposite points from " & _
               "the center are at the exact same dictance from that center."
    End Function

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

After creating a class, its private members are not accessible to clients of the class, not even to the children of that class. If you attempt to access a private member of a class outside of that class, you would receive an error. That what would happen with the following program:

Imports System

Public Class Circle

    Public Radius As Double

    Private Function ShowDescription()
        Return "A circle is a round geometric shape constructed " & _
               "so that all considered points of the shape are " & _
               "at an equal distance from a common point called " & _
               "the center. Also, two equally opposite points from " & _
               "the center are at the exact same dictance from that center."
    End Function

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

Public Class Sphere
    Inherits Circle

    Public Sub ShowCharacteristics()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 35.84

        Console.WriteLine("Circle Characteristics")
        Console.WriteLine("=======================================================")
        Console.WriteLine("Description:      {0} ", ShowDescription())
        Console.WriteLine("Radius:             {0}", Me.Radius)
        Console.WriteLine("Diameter:         {0}", Me.CalculateDiameter())
        Console.WriteLine("Circumference: {0}", Me.CalculateCircumference())
        Console.WriteLine("=======================================================")
    End Sub

End Class

Class Exercise

    Public Shared Sub main()

        Dim ball As Sphere = New Sphere

        ball.ShowCharacteristics()

    End Sub

End Class

 

 

Protected Members

When creating a class that would be used as the base of other classes, in some cases, you may want to create a special relationship among a class and its eventual children. For example, you may want to create some members of the parent class that only its derived class can access. These types of members must be created with the Protected keyword. Here is an example:

Public Class Circle

    Protected Radius As Double

    Private Function ShowDescription()
        Return "A circle is a round geometric shape constructed " & _
               "so that all considered points of the shape are " & _
               "at an equal distance from a common point called " & _
               "the center. Also, two equally opposite points from " & _
               "the center are at the exact same dictance from that center."
    End Function

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

Not just the member variables but you can also created methods as protected as long as you intend it only for the class and its children. Remember that a protected member cannot be accessed by the clients of the class, only by the class itself and its children.

When accessing the protected members of a class from its children, you can use Me to locate those members: Me gives you access to non-Shared public and protected members of both the parent(s) and its class. Here is an example:

Imports System

Public Class Circle

    Protected Radius As Double

    Protected Function ShowDescription()
        Return "A circle is a round geometric shape " & vbCrLf & _
               "constructed so that all considered points of the " & vbCrLf & _
               "shape are at an equal distance from a common point " & vbCrLf & _
               "called the center. Also, two equally opposite points " & vbCrLf & _
               "from the center are at the exact same dictance from " & vbCrLf & _
               "that center."
    End Function

    Public Function CalculateDiameter() As Double
        Return Radius * 2
    End Function

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

End Class

Public Class Sphere
    Inherits Circle

    Public Sub ShowCharacteristics()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 35.84

        Console.WriteLine("Circle Characteristics")
        Console.WriteLine("=======================================================")
        Console.WriteLine("Description:   {0}", Me.ShowDescription())
        Console.WriteLine("-=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=-")
        Console.WriteLine("Radius:        {0}", Me.Radius)
        Console.WriteLine("Diameter:      {0}", Me.CalculateDiameter())
        Console.WriteLine("Circumference: {0}", Me.CalculateCircumference())
        Console.WriteLine("=======================================================")
    End Sub

End Class

Class Exercise

    Public Shared Sub main()

        Dim ball As Sphere = New Sphere

        ball.ShowCharacteristics()

    End Sub

End Class

 

 

Previous Copyright © 2004-2012, FunctionX Next