S
sougata12
Guest
Hi,
Please help me understand the role of the SHADOWS keyword in shadowing through inheritance.
My question is : When the DoSomething method in the derived class is called through c.UseMe(), the SHADOWS keyword is behaving differently compared to when the DoSomething method in the derived class is called directly through c.DoSomething(). Why is this difference in behaviour of the SHADOWS keyword?
I wrote the following code and have explained my understanding below. Please tell me where I am going wrong:
1. STEP 1: Calls the useMe method in class PERSON
2. STEP 2: Me keyowrd behaves like an object referring to a current instance
3. STEP 3: Hence Me.DoSomething = c.DoSOmething and hence control should move to derived class
4. STEP 4: Once the control moves to the derived class, the compiler should execute the DoSomething in the derived class as it has shadowed the DoSomething in the base class. Hence the Output should be "Customer". But the output is "Person"
Module Module1
Sub main()
Dim c As New Customer()
c.UseMe() 'STEP 1
Console.WriteLine("----")
c.DoSomething()
Console.ReadLine()
End Sub
End Module
Public Class Person
Public Sub DoSomething()
Console.WriteLine("Person")
End Sub
Public Sub UseMe()
Me.DoSomething() 'STEP 2
'STEP 3
End Sub
End Class
Public Class Customer
Inherits Person
Public Shadows Sub DoSomething()
'STEP 4
Console.WriteLine("Customer")
End Sub
End Class
The output I am getting is as follows:
The control flow, as per my understanding is as follows (its not happening like this although):
So requesting help to understand why SHADOWS behave on expected lines when we use c.DoSomething() but behaves differently (i.e. does not execute the derived class method) when called through ME in the statement c.UseMe().
Sougata Ghosh
Continue reading...
Please help me understand the role of the SHADOWS keyword in shadowing through inheritance.
My question is : When the DoSomething method in the derived class is called through c.UseMe(), the SHADOWS keyword is behaving differently compared to when the DoSomething method in the derived class is called directly through c.DoSomething(). Why is this difference in behaviour of the SHADOWS keyword?
I wrote the following code and have explained my understanding below. Please tell me where I am going wrong:
1. STEP 1: Calls the useMe method in class PERSON
2. STEP 2: Me keyowrd behaves like an object referring to a current instance
3. STEP 3: Hence Me.DoSomething = c.DoSOmething and hence control should move to derived class
4. STEP 4: Once the control moves to the derived class, the compiler should execute the DoSomething in the derived class as it has shadowed the DoSomething in the base class. Hence the Output should be "Customer". But the output is "Person"
Module Module1
Sub main()
Dim c As New Customer()
c.UseMe() 'STEP 1
Console.WriteLine("----")
c.DoSomething()
Console.ReadLine()
End Sub
End Module
Public Class Person
Public Sub DoSomething()
Console.WriteLine("Person")
End Sub
Public Sub UseMe()
Me.DoSomething() 'STEP 2
'STEP 3
End Sub
End Class
Public Class Customer
Inherits Person
Public Shadows Sub DoSomething()
'STEP 4
Console.WriteLine("Customer")
End Sub
End Class
The output I am getting is as follows:
The control flow, as per my understanding is as follows (its not happening like this although):
So requesting help to understand why SHADOWS behave on expected lines when we use c.DoSomething() but behaves differently (i.e. does not execute the derived class method) when called through ME in the statement c.UseMe().
Sougata Ghosh
Continue reading...