• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding rules for static method hiding  RSS feed

 
poorvika chanda
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends
I had a doubt since long time ,inspite of going through some java articles and posting in java ranch not convincing.

my doubt is

"According to Java Spec Static methods cannot be overridden.fine.But why rules for overriding are applicable to in hiding of Static methods.".

Next is Overriding rules are applicable for hiding of static methods.But the Overriding rules are not required for hiding of Static variables.Why it is like that.

I am not able to get the exact reason.

Please help me.

Regards
Poorvika
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
poorvika chanda wrote:"According to Java Spec Static methods cannot be overridden.fine.But why rules for overriding are applicable to in hiding of Static methods.".
Next is Overriding rules are applicable for hiding of static methods.But the Overriding rules are not required for hiding of Static variables.Why it is like that.

Simply put, overriding only applies to methods. There is no such thing as 'overriding a variable'.

If class Parent defines a variable X, and class Child (which extends Parent) also defines a variable X, then Child's X will mask (or hide) Parent's. Assuming Parent's field is not private, it will still exist and can be accessed from Child with 'super.X', just not from outside.

Since static variables can always be accessed by putting the class name in front, masking really doesn't apply. The only thing is that an unqualified access from inside Child will always refer to it's own copy.

To be honest, the best thing to do is avoid this sort of thing in the first place.

Winston
 
poorvika chanda
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.it answered my second point.Can you please explain about my first question.I am eagerly waiting for answer for my first question
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
poorvika chanda wrote:Thanks for the reply.it answered my second point.Can you please explain about my first question.I am eagerly waiting for answer for my first question

Which is? I have to admit, I didn't quite follow it all.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!