aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Method Shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Method Shadowing" Watch "Regarding Method Shadowing" New topic
Author

Regarding Method Shadowing

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Can someone give me an example of method shadowing ?
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
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

Swati Kale
SCJP
SCWCD
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
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

Joined: Jul 08, 2001
Posts: 44
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

Joined: Jul 15, 2001
Posts: 112
Hi Joseph,
I think for the second option it is both the methods of sub class.
JOSEPH BIH
Ranch Hand

Joined: Jul 08, 2001
Posts: 44
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

Joined: Aug 06, 2001
Posts: 8
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Apr 14, 2001
Posts: 327
Thanks everyone .
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Sathya,
u interpreted the code correctly..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Method Shadowing
 
Similar Threads
methods that are static AND final
Static and Instance fields
Inheriting a protected member
Odd NullPointerExceptions
private method...