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:
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
|
Inheritance
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment