• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Hiding Static variables/methods using instance variables/methods

 
Greenhorn
Posts: 23
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

Why is it allowed to "hide" static variables using instance variables in a sub class when we are not able to do the same in case of methods.

The following compile time errors are encountered:

1. "This instance method cannot override the static method from A". - Generated when I try to redefine a static method of class A using an instance method in class B having the same signature.
2. "This static method cannot hide the instance method from A". - Generated when I try to override an instance method of class A using a static method in class B having the same signature.

In the above case, if I use instance and static variables to hide each other, it works like charm
Why doesn't it work in the case of methods ???

I have searched it in other forums/web sites but haven't yet seen a convincing answer (or maybe I wasn't able to understand).

Can anybody please explain? Thanks
 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Static member has no relation with the instance of class

so there is no concept of overriding in case of static method,that is why we cannot hide methods
when you have a sub class that extends a super class which has static method, then this static method cannot be override in the sub class
if you are doing so then it is the method of that sub class but not a overridden method
and we have to use the name of class to access that method

Hope you will get my point


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unlike methods, you cant override an instance variables!
 
Marshal
Posts: 74712
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if you can get your hands on a copy of Java™ Puzzlers by Joshua Bloch and Neal Gafter. That has several sections in about hiding and shadowing. I can’t remember the details (and haven’t got my copy with me at the moment), but start about page 180. It is generally not good practice to hide or shadow class members, except in constructors and set methods like thisIf you are using the @Override annotation, that will prompt the compiler to check carefully, producing errors if the methods are not overriding. The intention behind the @Override annotation is for the compiler to pick up unintentional overloading or spelling errors, as in this bit of code.Note we have an FAQ about overriding and hiding.
 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Well... my question is a bit different. I very clearly understand that static methods cannot be "overridden" and I also understand very clearly that variables (whether they are instance variables or static variables) cannot be overridden.

I dont want to override any of those.

All i am asking is, why is there a "discrepancy" when it comes to "hiding" methods and variables using static and non-static contexts together in the code. I am able to hide a static variable using an instance variable, but I am not able to hide a static method using an instance method ( and vice-versa).

Why ?

@Gursewak... Bro, we can hide static methods of a superclass using a static method of the same signature, compatible return types and throws clause in a subclass. It works fine. My question is ...
Why can't we do the same "hiding" using instance methods in the subclass. I do understand that static methods have got nothing to do with an instance of the class, but I don't know how valid an explanation it would be in this case. I am able to hide static variables of a super class using instance variables in a subclass. Why can't i do so (hiding static methods of a superclass ) using instance methods in a subclass ?

@Seetha... I dont want to. I just wanted to "hide" static vars using instance vars, and I am able to do so. But not in the case of methods (using static/non-static contexts together). Why ?

@Campbell ... was just trying to find out the technical feasibility and answers to the "hows" and "whys" in this case. I surely will get hold of the book - Java Puzzlers, though.

Thank you all for your posts !! Cheers :-)
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For argument's sake lets say if overriding of static methods are allowed and that you don't get the two errors posted in the first post - consider the below case of polymorphism.


Instance methods must be called using the instance. Is there a way you can call the child instance method someMethod() polymorphically?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandy Singh wrote:Hi Guys,

Well... my question is a bit different. I very clearly understand that static methods cannot be "overridden" and I also understand very clearly that variables (whether they are instance variables or static variables) cannot be overridden.

I dont want to override any of those.

All i am asking is, why is there a "discrepancy" when it comes to "hiding" methods and variables using static and non-static contexts together in the code. I am able to hide a static variable using an instance variable, but I am not able to hide a static method using an instance method ( and vice-versa).

Why ?



So your asking why this is legal:


but this is not


Yes?

Since the child inherits from the parent, and since static methods can't be overridden, you'd have two methods in the child with the same signature, one static and one not. Sure, the language could have made a rule that the static one is hidden in that case, but that complicates the rules for dynamic binding, would be an extra point of confusion for programmers, and, I suspect, would make the JLS and JVMS more difficult to implement.

The only way to get a concrete answer to "Why was decision X made?" though would be to ask the people who made it.
 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff, thats exactly what I was asking. Thanks for the explanation, precise and crisp
 
reply
    Bookmark Topic Watch Topic
  • New Topic