stu nicholls dot com | menu - Professional dropdown #3

click here


Overloading:
Overloaded members provide different versions of a property or method that have the same name, but that accepts different number of parameters.

Overridden:
Overridden properties and methods are used to replace an inherited property or method. When you override a member from a base class, you replace it. Overridden members must accept the same data type and number of arguments.

Shadowed:
Shadowed members are used to create a local version of a member that has broader scope. You can shadow a type with any other type. For example, you can declare a property that shadows an inherited method with the same name

Overloading Base class members
You can use the keyword Overloads to indicate that you are overloading a method, but the keyword is not necessary when you overload the members of the same class, but it is when you are overloading a method or property from other class such as base class. In such cases you must use the keyword Overloads. Following example demonstrates it.
Public Class NewExampleOverload
Inherits System.Windows.Forms.Form
Dim dolly As child
Dim jolly As adult
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dolly = New child(Me)
dolly.dancing()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dolly.crying()
Catch
MsgBox("Dolly is not born")
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
jolly = New adult(Me)
jolly.dancing("Bharatha Natyam....")
End Sub
End Class
Public Class Human
Public mainform As New NewExampleOverload()
Public Sub New(ByRef form1 As NewExampleOverload)
mainform = form1
End Sub
Public Sub dancing()
mainform.TextBox1.Text = "Dancing"
End Sub
End Class
Public Class child
Inherits Human
Public Sub New(ByRef form1 As NewExampleOverload)
MyBase.New(form1) End Sub
Public Sub crying()
mainform.TextBox1.Text = "Crying"
End Sub
End Class
Public Class adult
Inherits Human
Public Sub New(ByVal form1 As NewExampleOverload)
MyBase.New(form1)
End Sub
Public Overloads Sub Dancing(ByVal text As String)
mainform.TextBox1.Text = text
End Sub
End Class

Overriding Base Class members:
You can use the keyword Overloads to indicate that you are overloading a method, but the keyword is not necessary when you overload the members of the same class, but it is when you are overloading a method or property from other class such as base class. In such cases you must use the keyword Overloads. Following example demonstrates it.
Public Class NewExampleOverridable
Inherits System.Windows.Forms.Form
Dim dolly As child
Dim jolly As adult
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dolly = New child(Me)
dolly.dancing()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dolly.crying()
Catch
MsgBox("Dolly is not born")
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
jolly = New adult(Me)
jolly.dancing()
End Sub
End Class
Public Class Human
Public mainform As New NewExampleOverridable()
Public Sub New(ByRef form1 As NewExampleOverridable)
mainform = form1
End Sub
Public Overridable Sub dancing()
mainform.TextBox1.Text = "Dancing"
End Sub
End Class
Public Class child
Inherits Human
Public Sub New(ByRef form1 As NewExampleOverridable)
MyBase.New(form1)
End Sub
Public Sub crying()
mainform.TextBox1.Text = "Crying"
End Sub
End Class
Public Class adult
Inherits Human
Public Sub New(ByVal form1 As NewExampleOverridable)
MyBase.New(form1)
End Sub
Public Overrides Sub Dancing()
mainform.TextBox1.Text = "Bharatha Natyam..."
End Sub
End Class

MustOverride, Overridable, Notoverridable
Overridable:

This indicates that the method can be overridden


MustOverride:

This is used to indicate that the derived class must provide their own implementation of a method

public MustOverride Overridable Sub Dancing()

Using a MustOverride creates a pure virtual, also called abstract methods

NotOverridable:

This can be used when a method should not be overridden
public NotOverridable Sub Dancing()


Shadowing

In this concept you can have programming elements in the same module, class, with the same name but different scopes. When you have such elements, and the code refers to the name they share, the compiler uses the element with the narrower, closer scope.
For example, Imagine a module defines a public variable named Data, and a procedure inside the module declares a local variable also named Data. When you use the name Data in the procedure, you use the local variable, but references to Data outside the procedure, you use the public variable. Here the procedure level variable Data shadows the module level variable Data
If the derived class redefines an element from a base class, the redefined element shadows the original element. When you access a shadowed element in a derived class, you can do so by qualifying its name with Me. If your derived class shadows the element in the base class, you can access the base class element by qualifying it MyBase. You can redefine the elements with the keyword shadows
Note: You can use MyBase keyword to access methods in a base class when overriding methods in a derived class.

Example Module Module1
Dim ob As New class2()
Class class1
Public Data As Double = 55.5
Public a1 As Integer = 125
End Class
Class class2
Inherits class1
Shadows a1 As Integer = 105
Public Shadows ReadOnly Property Data() As Double
Get
Return 65.5
End Get
End Property
Sub test()
Dim f As Double
Dim a2 As Integer
f = Me.Data()
System.Console.WriteLine("value from derived class " & f)
f = MyBase.Data
System.Console.WriteLine("Value from base class” & f)
a2 = MyBase.a1
System.Console.WriteLine("value from base class" & a2)
a2 = a1
System.Console.WriteLine("value from derived class " & a2)
End Sub
End Class
Sub Main()
ob.test()
End Sub
End Module

Inheritance<< Previous   Next>>Interfaces


comments powered by Disqus
click here
click here
Footer1