Method Overloading in vb.net
Method Overloading:
Whenever the definition of the base class is modified at the derived class then it is said to be method overriding.
If you want to change definition than you must use the keyword “Overrides”
Overrides change the definition of base class.
Note:
In order to modify the definition of the base class at the derived class then the method should be prefixed with “Overrides” keyword.
Note:
If any method is defined using “Overrides” keyword at the derived class then the method should be prefixed with “Overridable” keyword at the base class.
Note:
If any method is prefixed with “Overridable” keyword at the base class then it is not mandatory to redefine the definitions at the derived class.
‘‘Demo on Method Overriding
Imports system
Module Method Overriding ‘‘Demo
Class Parents
Public Overridable sub MyProperty ( )
Console.WriteLine (“Use property for business”)
End sub
End class
Class Children overrides sub MyProperty ( )
MyBase.Hyproperty ( ) invokes the base class my property method
Console.WriteLine (“Use property for charity”)
End sub
End class
Sub main ( )
Dim c as New Children
C.MyProperty ( )
End sub
End Module
Shadowing:
It is used to maintain two definitions at the derived class such that one definition can be shadowed in order to project the other definition i.e., the definition of the base class can be shadowed in order to project the new definition provided at the derived class and vice versa is also applicable.
Observation:
- In order to shadow the definition at the derived class shadows keyword should be used.
- If an object is created for the derived class then the definition provided at the derived class will be projected.
- In order to retrieve the definition of the base class using the derived class object.
To Do:
- Define a variable for the base class
- Create an instance for the derived class and assign to the base class variable invoke the members using the base class variable.
‘‘Demo on Shadowing
Imports system
Module Shadowing ‘‘Demo
Class Parents
Public sub MyProperty ( )
Console.WriteLine (“Use property for business”)
End sub
End class
Class children
Inherits Parents
Public Shadows sub MyProperty ()
Console.WriteLine (“Use property for charity”)
End sub
End class
Sub main ( )
Dim c as New Children
C.MyProperty ( )
Dim P as Parents
P.MyProperty ( )
End sub
End Module