Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding in Static Methods

 
Vaibhav Shridish
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
priya selva
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
static methods cannot be overridden to be Non static...
But static methods can be overridden as static...

refer page 87 of R&H
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vaibhav Shridish
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
STATIC METHODS CAN NOT BE OVERRIDDEN! Static methods are only hidden.
What do you mean with hidden ???
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great ans that covey but in such a case how do u access the overridden static method ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic