Shadowing
Shadowing is a concept seen when inheritance is brought into effect.
When you have a base class and a derived class, inheritance inherits all the exposed functionalities of the base class to the derived class. This means now you can implement all the public or protected functions or methods in the derived class to extend the functionality of the base function. Alternatively, if we shadow a function in the derived class, we has the function with the same name as with the base class but shadowed method cannot be accessed from the object of the base class. Whereas overridden method in the derived class can be accessed from the object of the base class. To access an shadowed method, you have to create object of the derived class and it cannot be accessed through the base class object.
Let me give you an example:
Class Math
{
virtual sub Add(int a, int b);
}
Class MathExt
{
overrides sub Add(int a, int b){....};
shadows sub Add(double a, int b){.....};
}
Main
{
Math m1 = new Math();
MathExt me1 = new MathExt();
int a;
int b;
float c;
m1.Add(a,b)----> this call ultimately lands in the derived class calling the function with same signature and overrides keyword.
m1.Add(c,b)----> this gives an error as base and its derived class has no overridden function of the same signature
me1.Add(c.b)---> works
}
|