Home

Polymorphism

 

Polymorphism Fundamentals

 

Introduction

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:

<script language="VB" runat="server">
Public Class Circle
    Protected Radius As Double

    Public Function Describe$()
        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 distance 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

    Public Function Present$()
	Return "Circle Characteristics<br />" & _
               "---------------------------------<br />" & _
               "Description: " & Me.ShowDescription() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius: " & CStr(Me.Radius) & "<br />" & _
               "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		   CStr(Me.CalculateCircumference()) & "<br />" & _
               "========================="
    End Function
End Class

Public Class Sphere
    Inherits Circle

    Public Function Present$()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)

        Return "Sphere Characteristics<br />" & _
               "---------------------------------<br />" & _
               "Description:   " & Me.ShowDescription() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius:        " & CStr(Me.Radius) & "<br />" & _
               "Diameter:      " & _
		CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		CStr(Me.CalculateCircumference()) & "<br />" & _
               "========================="
    End Function
End Class
</script>

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:

<script language="VB" runat="server">
Public Class Circle
    Protected Radius As Double

    Public Overridable Function Describe$()
        Return ""
    End Function

End Class

Public Class Sphere
    Inherits Circle

End Class
</script>

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:

<%@ Page Language="VB" %>
<html>
<head>

<script language="VB" runat="server">
Public Class Circle
    Public Radius As Double

    Public Overridable Function Describe$()
        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 distance 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

    Public Overridable Function Present$()
	Return "Circle Characteristics<br />" & _
               "-------------------------------------<br />" & _
               "Description: " & Me.Describe$() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius: " & CStr(Me.Radius) & "<br />" & _
               "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		   CStr(Me.CalculateCircumference()) & "<br />" & _
               "=========================="
    End Function
End Class

Public Class Sphere
    Inherits Circle

    Public Overrides Function Describe$()
        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 Function Present$()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)

        Return "Sphere Characteristics<br />" & _
               "---------------------------------------<br />" & _
               "Description:   " & Me.Describe$() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius:        " & CStr(Me.Radius) & "<br />" & _
               "Diameter:      " & _
		CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		CStr(Me.CalculateCircumference()) & "<br />" & _
               "=========================="
    End Function
End Class
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Round As Circle = New Circle

    Round.Radius = 44.85
    Response.Write(Round.Present$())
    Response.Write("<br />")

    Dim Ball As Sphere = New Sphere

    Ball.Radius = 28.92
    Response.Write(Ball.Present$())
%>

</body>
</html>

This would produce:

Overrides

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:

Public 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 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:

<%@ Page Language="VB" %>
<html>
<head>

<script language="VB" runat="server">
Public Class Circle
    Public Radius As Double

    Public Overridable Function Describe$()
        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 distance 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

    Public Overridable Function Present$()
	Return "Circle Characteristics<br />" & _
               "---------------------------------------------" & _
	       "----------------------------------------<br />" & _
               "Description: " & Me.Describe$() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- " & _
	       "-=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius: " & CStr(Me.Radius) & "<br />" & _
               "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		   CStr(Me.CalculateCircumference()) & "<br />" & _
               "========================================================="
    End Function
End Class

Public Class Sphere
    Inherits Circle

    Public Overrides Function Describe$()
        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 Function Present$()
        ' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)

        Return "Sphere Characteristics<br />" & _
               "---------------------------------------------" & _
	       "----------------------------------------<br />" & _
               "Description:   " & Me.Describe$() & "<br />" & _
	       "=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- -=- " & _
	       "-=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _
               "Radius:        " & CStr(Me.Radius) & "<br />" & _
               "Diameter:      " & _
		CStr(Me.CalculateDiameter()) & "<br />" & _
               "Circumference: " & _
		CStr(Me.CalculateCircumference()) & "<br />" & _
               "Area:          " & CStr(Me.CalculateArea()) & "<br />" & _
               "Volume:        " & CStr(Me.CalculateVolume()) & "<br />" & _
               "========================================================="

    End Function
End Class
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Round As Circle = New Circle

    Round.Radius = 44.85
    Response.Write(Round.Present$())
    Response.Write("<br />")

    Dim Ball As Sphere = New Sphere

    Ball.Radius = 28.92
    Response.Write(Ball.Present$())
%>

</body>
</html>

This would produce:

Inheritance

 
 
   
 

Previous Copyright © 2008-2013 FunctionX, Inc. Next