After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example: Module Exercise Public Function Main() As Integer Dim ball As Sphere = New Sphere Return 0 End Function End Module 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: Code File: Circle.vbPublic 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 Sub ShowCharacteristics() ' Because Sphere is based on Circle, you can access ' any public member(s) of Circle without qualifying it(them) Radius = 35.84 Console.WriteLine("Circle Characteristics") Console.WriteLine("=======================================================") Console.WriteLine("Radius: {0}", Radius) Console.WriteLine("Diameter: {0}", CalculateDiameter()) Console.WriteLine("Circumference: {0}", CalculateCircumference()) Console.WriteLine("=======================================================") End Sub End Class Code File: Exercise.vbModule Exercise Public Function Main() As Integer Dim ball As Sphere = New Sphere ball.ShowCharacteristics() Return 0 End Function End Module This would produce: Sphere Characteristics Radius: 35.84 Diameter: 71.68 Circumference: 225.1891712 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 is an example: 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 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("Circle Characteristics") Console.WriteLine("=======================================================") Console.WriteLine("Radius: {0}", Me.Radius) Console.WriteLine("Diameter: {0}", Me.CalculateDiameter()) Console.WriteLine("Circumference: {0}", Me.CalculateCircumference()) Console.WriteLine("=======================================================") End Sub End Class 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: Public Class Circle Public Radius As Double Private Function ShowDescription() 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 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 what would happen with the following program: Public Class Circle Public Radius As Double Private Function ShowDescription() 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 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("Circle Characteristics") Console.WriteLine("=======================================================") Console.WriteLine("Description: {0}", ShowDescription()) Console.WriteLine("Radius: {0}", Me.Radius) Console.WriteLine("Diameter: {0}", Me.CalculateDiameter()) Console.WriteLine("Circumference: {0}", Me.CalculateCircumference()) Console.WriteLine("=======================================================") End Sub End Class 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: Public Class Circle Protected Radius As Double Private Function ShowDescription() 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 Not just the member variables but you can also create methods 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: Public Class Circle Protected Radius As Double Protected Function ShowDescription() 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 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("Circle 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("=======================================================") End Sub End Class
|
|
|||||||
|