This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
JAVACAPS ... 4. What is the output when you compile and run the following code fragment?
A) Printing myTest in MyTest class followed by Printing myStat in MyTest class B) Printing myTest in Test class followed by Printing myStat in MyTest class C) Printing myTest in MyTest class followed by Printing myStat in MyTest class D) Printing myStat in MyTest class followed by Printing myStat in MyTest class Answer 4: WHEN STATIC METHODS CANNOT BE OVERRIDDEN HOW IS THAT THIS HAS BEEN OVERRIDDEN ? [ Jess added UBB [CODE] tags to preserver whitespace, improve readibility ] [ June 06, 2002: Message edited by: Jessica Sant ]
Vaibhav Shridish
priya selva
Greenhorn
Joined: Feb 21, 2002
Posts: 29
posted
0
Hi, static methods cannot be overridden to be Non static... But static methods can be overridden as static...
Two things: 1st thing: Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy. You can change it here. 2nd thing:
Originally posted by Vaibhav:
D) Printing myStat in MyTest class followed by Printing myStat in MyTest class Answer 4:
Check your typing of the answer -- I think you put # 4 wrong -- neither of the myTest() methods print the word "myStat" like it says in the first half of the answer. [ June 06, 2002: Message edited by: Jessica Sant ]
Thanks priya,jessica Frankly speaking i am following only 1 book that is Mughal and Rasmussen.What is this book R & H ? ================================================= And Jessica sorry that was question Number i did not write any ans there ... =================================================
Archana Agrawal
Greenhorn
Joined: May 14, 2002
Posts: 23
posted
0
hello, My question is that if static method are overriden by static method,thats fine.....but whats the use of overriding these methods,when we are refering to the parent static method....
Thanks..........
Paul Villangca
Ranch Hand
Joined: Jun 04, 2002
Posts: 133
posted
0
Can a subclass inherit static members (variables and methods) from the superclass? 'Coz I read somewhere that static methods are not overriden, just hidden.
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Originally posted by Paul Villangca: Can a subclass inherit static members (variables and methods) from the superclass? 'Coz I read somewhere that static methods are not overriden, just hidden.
First thing, and this is very important, so listen up: STATIC METHODS CAN NOT BE OVERRIDDEN! Static methods are only hidden. In addition, all accessible members, excluding constructors, are inherited. This is regardless of the fact that the members may or may not be static. Corey
STATIC METHODS CAN NOT BE OVERRIDDEN! Static methods are only hidden. What do you mean with hidden ???
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Method overriding implies that dynamic binding is used to determine which method will be invoked at runtime. This is not the case with static methods. Let's look at the following example:
You can see from this example that both methods are, indeed, inherited by class Sub. Now, let's define methods within Sub that have the same signature as the methods in Super and see what happens:
As you can see, dynamic binding is used for the instance method because Sub's instanceMethod() was invoked. However, when we invoked s.staticMethod(), we ended up invoking the staticMethod within Super. Why is this? That's because the static method isn't overridden. Method overriding only applies to instance methods. Static methods are said to be hidden. Check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for all sorts of information on this topic. I hope that helps, Corey
Vaibhav Shridish
Greenhorn
Joined: Jun 06, 2002
Posts: 28
posted
0
Great ans that covey but in such a case how do u access the overridden static method ?
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Originally posted by Vaibhav Shridish: ...overridden static method...
NO! NO! NO! The static method IS NOT OVERRIDDEN! It is hidden! It's like a mantra. Repeat this to yourself 80,000 times a day and you'll be fine. When you use a static invocation mode with an object reference, the compile-time type of the object is used to determine which method to invoke. That's why Super.staticMethod was invoked in the example above. The compile-time type of s is that of type Super. If you'd rather invoke the staticMethod of the Sub class, you'd have to either invoke it on a variable that has a compile-time type of Sub or use the name of the Sub class explicitly. Try this:
Run this and see what you come up with for output. If you have any more questions, let me know. Be sure to check out the JLS, §15.12 Method Invocation Expressions for gobs of details about this. Corey