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]
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.
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