|
Polymorphism |
|
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-2007 FunctionX, Inc. | Next |
|
|
||