aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shadowing static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shadowing static methods" Watch "Shadowing static methods" New topic
Author

Shadowing static methods

John Held
Greenhorn

Joined: Jun 13, 2002
Posts: 13
The following code compiles and apparently is an example of shadowing a static method. It sure looks like a static method overwriting another static method to me.
Does anyone have any insights?
public class A
{
public static void sM1() { System.out.println("In base static"); }
}
class B extends A
{
public static void sM1() { System.out.println("In sub static"); }

}


John Held
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

but you're not overriding it -- you're hiding it -- if you were overriding it, you'd be able to call super.sM1()... but you can't:

[ July 12, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Here's a question from my mock exam that illustrates the difference between overriding an instance method and hiding a static method. Which answer do you believe is correct?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
When I compiled and ran this program, I get a totally unexpected result... it is P.prints1 Q.prints2. Can you please explain me.
Thanks
Charu
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Static methods are known at compile time itself.



prints1() method of P class is done at compile time.
Therefore, printS1S2() invokes prints1() of P class.


Val SCJP2 1.2<br />====>SCJD
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Charu,
Yes, I can explain the results.
This question from my mock exam is an example of the differences between overriding inherited instance methods and hiding static methods of the super class. The explanation appears below the code.

Class Q inherits method printS1S2 from class P. The method printS1S2 invokes methods printS1 and printS2. Since printS1 is static the reference to it is assigned at compile time and therefore P.printS1 is invoked. However, printS2 is an instance method so the reference to it is not computed until run time when printS1S2 is invoked as an inherited member of class Q. As a result, Q.printS2 is invoked.
Method Q.printS1 hides P.printS1. However, Q.printS2 overrides P.printS2.
If you would like to work with more questions ralated to inheritance, then you might want to try my mock exam at the URL contained in my signature. On the home page, you will see two versions of the exam, July 4 and July 8. You should always use the latest version. After you click on July 8, then go to the topic exams and select inheritance. There are 10 questions in the inheritance topic and all of them have explanations.
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
how can a non static method printS1S2() call Static method printS1();?
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
non-static methods, i.e. instance methods can call static methods, but not the other way round.
i.e. static methods can only call other static methods.


Amir
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
Thanks Dan. Now I got it.
Charu.
John Held
Greenhorn

Joined: Jun 13, 2002
Posts: 13
Thanks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shadowing static methods