Home

Inheritance

 

Introduction to Inheritance

 

Definition

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

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

<script language="VB" runat="server">
Public 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
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim circ As Circle = New Circle
    circ.Radius = 25.84

    Response.Write(" -=- Circle Characteristics -=-<br />")
    Response.Write("Radius: " & CStr(circ.Radius) & "<br />")
    Response.Write("Diameter: " & _
		   CStr(circ.CalculateDiameter()) & "<br />")
    Response.Write("Circmuference: " & _
		   CStr(circ.CalculateCircmuference()) & "<br />")
    Response.Write("Area: " & CStr(circ.CalculateArea()))
%>

</body>
</html>

This would produce:

Inheritance

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:

Public Class Circle

End Class

Class Sphere
    Inherits Circle

End Class

If you want to be able to access the class from other languages, you can precede its name with the Public keyword:

Public Class Circle

End Class

Public Class Sphere
    Inherits Circle

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:

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

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

End Class

Public Class Sphere
    Inherits Circle

End Class
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim ball As Sphere = New Sphere
%>

</body>
</html>

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:

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

<script language="VB" runat="server">
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 Function Describe$()
	' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Radius = 28.55

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

<title>Exercise</title>

</head>
<body>

<%
    Dim Ball As Sphere = New Sphere

    Response.Write(Ball.Describe())
%>

</body>
</html>

This would produce:

Inheritance

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 are examples:

<script language="VB" runat="server">
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 Function Describe$()
	' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 22.855

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

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:

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

    Private 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 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

</script>

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's what would happen with the following program:

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

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

    Private 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 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 Function Describe$()
	' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 22.855

	Return "Circle Characteristics<br />" & _
               "---------------------------------<br />" & _
               "Description: " & Describe$() & "<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 Ball As Sphere = New Sphere

    Response.Write(Ball.Describe())
%>

</body>
</html>

This would produce:

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:

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

    Private 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 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
</script>

Not just the member variables but you can also create a method 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:

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

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

    Protected 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 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 Function Describe$()
	' Because Sphere is based on Circle, you can access
        ' any public member(s) of Circle without qualifying it(them)
        Me.Radius = 22.855

	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
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Ball As Sphere = New Sphere

    Response.Write(Ball.Describe())
%>

</body>
</html>

This would produce:

Inheritance

 
 
   
 

Home Copyright 2009 FunctionX, Inc. Next