Home

Polymorphism

 

Introduction

As mentioned already, inheritance allows you to reuse, in a child class, code that is already implemented in a parent class. In some cases, that original code will be enough. In some other cases, you will need to apply new behavior to a child class even though the parent class already has a behavior close to what you seek. Re-creating a method in a child class using the same signature (name and arguments, if any) of a method of a parent class is referred to as overriding. Based on this, if a derived and a parent classes have the same member but implemented differently, when accessing that in your code, you need to know what particular member you are referring to, the parent or the derived's. This is the basis of polymorphism.

Overriding a Method

When creating the above two classes, imagine that you want to create a method that displays the characteristics of each shape. The method would belong to its corresponding class. This can be done as follows:

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

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

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)

        Console.WriteLine("Sphere 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()

    End Sub

End Class

When creating a class such as the above circle, if you create a member that the class' children would override, you can (should/must) indicate this to the compiler. This is done by preceding it with the Overridable keyword. This would be done as follows:

Imports System

Module Exercise

    Private Class Circle

        Public Ovirridable Sub ShowCharacteristics()
            
        End Sub
    End Class

    Private Class Sphere
        Inherits Circle

        Public Sub ShowCharacteristics()
            
        End Sub
    End Class

    Public Sub Main()
        
    End Sub

End Module

Once the member has been marked as overridable, when implementing the child class, in order to override it, you must mark the corresponding member of the child class as override. To do this, precede it with the Overrides keyword. This would be done as follows:

Imports System

Public Class Circle

    Protected Radius As Double

    Protected Overridable 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

    Public Overridable Sub ShowCharacteristics()
        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}" & vbCrLf, Me.CalculateCircumference())
    End Sub

End Class

Public Class Sphere
    Inherits Circle

    Protected Overrides Function ShowDescription()
        Return "A sphere is a three-dimensional geometric " & vbCrLf & _
               "shape based on a circle. It is constructed " & vbCrLf & _
               "so that all considered points around the shape " & vbCrLf & _
               "are at an equal distance from a common point " & vbCrLf & _
               "called the center. Like the circle, two equally " & vbCrLf & _
               "opposite points from the center are at the exact " & vbCrLf & _
               "same dictance from that center."
    End Function

    Public Overrides 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("Sphere 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 circ As Circle = New Circle
        circ.ShowCharacteristics()

        Dim ball As Sphere = New Sphere
        ball.ShowCharacteristics()

    End Sub

End Class

This would produce:

Circle Characteristics
=======================================================
Description:   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.
-=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=-
Radius:             35.84
Diameter:         71.68
Circumference: 225.1891712

Sphere Characteristics
=======================================================
Description:   A sphere is a three-dimensional geometric
shape based on a circle. It is constructed
so that all considered points around the shape
are at an equal distance from a common point
called the center. Like the circle, two equally
opposite points from the center are at the exact
same dictance from that center.
-=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=-
Radius:             35.84
Diameter:         71.68
Circumference: 225.1891712
=======================================================

If you mark as member of a child class with Overrides, it must have a corresponding member in the parent class and that corresponding member must be marked with the Overridable keyword.

 
 

Shadowing a Method

You can notice in the above example that the derived class produces the same results as the base class. In reality, inheritance is used to solve various Object-Oriented Programming (OOP) problems. One of them consists of customizing, adapting, or improving the behavior of a feature of the parent class. For example, although both the circle and the sphere have an area, their areas are not the same. A circle is a flat surface but a sphere is a volume, which makes its area very much higher. Since they use different formulas for their respective area, you should implement a new version of the area in the sphere. This would be done as follows:

Imports System

    Private Class Circle
        Public Radius As Double

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

    End Class

    Private Class Sphere
        Inherits Circle

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

        End Sub

Class Exercise
    End Class

    Public Sub Main()
        
    End Sub

End Class

Imagine that, in a method of the Sphere class, you call an Area() method, even if you use Me, it may not appear clear what Area() you are accessing. If you create a member, such as a method, in the child class and that has the same signature as an existing member of a parent class, to make sure that you access the derived version of the member, you can hide the corresponding member of the parent class. To do this, precede the member of the child class with the Shadows keyword. This would be done as follows:

Imports System

Public Class Circle

    Protected Radius As Double

    Protected Overridable 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

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

    Public Overridable Sub ShowCharacteristics()
        Me.Radius = 35.84

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

End Class

Public Class Sphere
    Inherits Circle

    Protected Overrides Function ShowDescription()
        Return "A sphere is a three-dimensional geometric " & vbCrLf & _
               "shape based on a circle. It is constructed " & vbCrLf & _
               "so that all considered points around the shape " & vbCrLf & _
               "are at an equal distance from a common point " & vbCrLf & _
               "called the center. Like the circle, two equally " & vbCrLf & _
               "opposite points from the center are at the exact " & vbCrLf & _
               "same dictance from that center."
    End Function

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

    Public Function CalculateVolume() As Double
        Return 4 * 3.14159 * Radius * Radius * Radius / 3
    End Function

    Public Overrides 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("Sphere 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("Area:          {0}", Me.CalculateArea())
        Console.WriteLine("Volume:        {0}", Me.CalculateVolume())
        Console.WriteLine("=======================================================")
    End Sub

End Class

Class Exercise

    Public Shared Sub main()
        Dim circ As Circle = New Circle
        circ.ShowCharacteristics()

        Dim ball As Sphere = New Sphere
        ball.ShowCharacteristics()

    End Sub

End Class

This would produce:

Circle Characteristics
=======================================================
Description:   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.
-=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=-
Radius:        35.84
Diameter:      71.68
Circumference: 225.1891712
Area:          4035.389947904
=======================================================
Sphere Characteristics
=======================================================
Description:   A sphere is a three-dimensional geometric
shape based on a circle. It is constructed
so that all considered points around the shape
are at an equal distance from a common point
called the center. Like the circle, two equally
opposite points from the center are at the exact
same dictance from that center.
-=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=-
Radius:        35.84
Diameter:      71.68
Circumference: 225.1891712
Area:          16141.559791616
Volume:        192837.834310506
=======================================================
 

Previous Copyright © 2004-2016, FunctionX, Inc. Next