When
two elements in a program have same name, one of them can hide and shadow the
other
one.
So in such cases the element, which shadowed the main element, is referenced.
Below
is a sample code, there are two classes “Parent” and “ShadowedParent”. In
“Parent”,
there is a variable “x” which is a integer. “ShadowedParent” overrides
“Parent” and shadows the “x”
variable to a string.
Example:
Public
Class Parent
Public
x As Integer
End
Class
Public
Class ShadowedParent
Inherits
Parent
Public
Shadows x As String
End Class
Difference
between Shadowing and Overriding:
• Overriding
redefines only the implementation while shadowing redefines the whole
element.
• In
overriding derived classes can refer the parent class element by using “ME”
keyword,
but in shadowing you can
access it by “MYBASE”.
Post By : Urvish Patel
Blog By : Dipen Shah
Learn New...
No comments:
Post a Comment