Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Method Shadowing

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone give me an example of method shadowing ?
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In one of the recent post, someone (i don't remember name ) has given the following sample code, where it shows the shadowing of static methods.

In this example,there is shodowing of methodA.
I hope, this is what showding means.Honestly,I am not very sure..someone comment.
Thanx
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For static methods it is termed as "METHOD HIDING" rather than method overriding which is applicable to non-static methods/ instance methods.
do u refer to that?
tvs sundaram
 
JOSEPH BIH
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way of getting a feel is to walk through the code and see the output result.
In this case, main method gets executed when
Super s = new Super();
s.methodA();
s.methodB();
output should be the print statements of methodA() and methodB() respectively of the super class.
2.
StaticDemo sd = new StaticDemo();
sd.methodA();
sd.methodB();
output should be the print statements of methodA() of the super class (namely, static hiding)and methodB() of the sub class (overwriting methodB()in super class).
3.
Super s2 = new StaticDemo();
s2.methodA();
s2.methodB();
same as 1st exe given the super type class.
correct me if I am wrong.
Joseph
Originally posted by swati bannore:
[B]Hi,
In one of the recent post, someone (i don't remember name ) has given the following sample code, where it shows the shadowing of static methods.

In this example,there is shodowing of methodA.
I hope, this is what showding means.Honestly,I am not very sure..someone comment.
Thanx
[/B]

 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joseph,
I think for the second option it is both the methods of sub class.
 
JOSEPH BIH
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you cannot overwrite static mothodA() in super class, right?
Correct me if I am wrong.
Joseph
Originally posted by Rajani Katta:
Hi Joseph,
I think for the second option it is both the methods of sub class.

 
Sathya Ramanathan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
The Following is the output I got when I ran the program posted by Swati.
Method A from Super
MEthod B from Super
MEthod A from Sub
Method B from Sub
Method A from Super
Method B from Sub
I think the first 4 lines of output does not create much confusion with respect to the code.
Now consider the code
Super s2 = new StaticDemo();
Here a reference to Super is given by creating a instance of StaticDemo. It is to be noted that the methodA() in Super & StaticDemo class is Static and methodB()is not.
Now when methodB() is called using s2 ie, s2.methodB(), the compiler checks if the methodB() is existing in the super class, if not it gives a compile time error. But during run time the methodB() of the subclass(StaticDemo) is accessed and the output is printed to be "MethodB from Sub". Probably this is what is refered to as "Method Shadowing"
On the other hand when the static methodA() is called using s2 ie s2.methodA() then the methodA() of Super class is accessed during the run time and it provides the output "MethodA from Super"
But i am not sure of whether my reasoning is right. Any clarifications would be appreciated.
Sathya
Texas Tech University.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
This article on Shadowing at Sun may help. It explains, with examples, shadowing, hiding and obscuring.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just my 2 cents..
methodA is static, so overriding and all that wont work
so if you call it, it will go to the class
therefore you see the Super output
2nd case, its instance, so obvious, dynamic binding (overriding)
works and subclass method gets called
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone .
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathya,
u interpreted the code correctly..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic