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