This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Static Method and Inheritence.  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have read in few articles that the static methods cannot be inherited.

To prove to myself about what i have read,
I created a Parent Class with a static method say MethodA()
then i Created a Child Class inheriting the Parent Class

But It was a surprise for me when I saw that,
I was able to inherit the static method defined in the Parent Class??

Can anyone of you, share your thoughts on this topic....

Regards
[ November 10, 2004: Message edited by: Sujatha Kumar ]
 
author & internet detective
Marshal
Posts: 38508
653
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujatha,
You can create another static method in the child class with the same name. However, it is not inherited.

ParentClass.method() will execute the code in the parent class.
ChildClass.method() will execute the code in the child class.

You can test this out by trying to put the following code in the child class' copy of the static method:
super.method();

It should give you a compiler error. If you try this in a non-static method, it will compile and call the parent class' method.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output: Parent

It still prints Parent. I guess this means that the protected static void printMe() is inherited. It can also be overridden in the child class.

Nikhil
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doubt, check the JLS. Here are some excerpts that may be of interest.


Section 8.4.6.2
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.




Section 8.4.6.4
It is possible for a class to inherit more than one method with the same signature. Such a situation does not in itself cause a compile-time error. There are then two possible cases:

* If one of the inherited methods is not abstract, then there are two subcases:
o If the method that is not abstract is static, a compile-time error occurs.
o Otherwise, the method that is not abstract is considered to override, and therefore to implement, all the other methods on behalf of the class that inherits it. A compile-time error occurs if, comparing the method that is not abstract with each of the other of the inherited methods, for any such pair, either they have different return types or one has a return type and the other is void. Moreover, a compile-time error occurs if the inherited method that is not abstract has a throws clause that conflicts (�8.4.4) with that of any other of the inherited methods.
* If all the inherited methods are abstract, then the class is necessarily an abstract class and is considered to inherit all the abstract methods. A compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void. (The throws clauses do not cause errors in this case.)



Hope this helps.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sujatha Kumar:
Hi All,

I have read in few articles that the static methods cannot be inherited.

To prove to myself about what i have read,
I created a Parent Class with a static method say MethodA()
then i Created a Child Class inheriting the Parent Class

But It was a surprise for me when I saw that,
I was able to inherit the static method defined in the Parent Class??

Can anyone of you, share your thoughts on this topic....

Regards

[ November 10, 2004: Message edited by: Sujatha Kumar ]



Seems like you are talking about static {} blocks and not static methods. there is a difference between these two. static block is a class level block that always executed upon loading perticular class but it is not available to its children. I usually call it ghost code and try to refrain from it unless my class is final. Where as a static method is a method and no matter what as long as its public its available to children.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No he was talking about static methods And yeah they are not polymorphic. This is because when we invoke such method on object, the actual method invoked depends on object real class. Static method is not associated with an object. I think I have repeated that everybody was saying here

 
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujatha Kumar:

I have read in few articles that the static methods cannot be inherited.

Perhaps it would be clearer to say that static methods cannot be overridden.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nikhil Vasaikar:
It can also be overridden in the child class.



No, it cannot be overridden. Overriding implies polymorphism, and static methods aren't polymorph. Which exact method is called is decided at compile time, based on the reference type, not at runtime based on the object type.

Redefining a static method in a subclass is called "shadowing", if I remember correctly.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they could/should disallow this kind of shadowing in compiler. We can always chose another method name for inherited class.
 
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This conversation strikes me as an issue in the language and not one of general OO design. I will therefore move it to Intermediate Java.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vladas Razas:
I think they could/should disallow this kind of shadowing in compiler. We can always chose another method name for inherited class.



Checking this in the compiler doesn't suffice, as two classes used together at runtime don't need to have been compiled together.

If shadowing were disallowed, that would mean I could break a class simply by adding a static method to its superclass. Bad idea, in my opinion.
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So What's the conclusion?

Static methods are not inherited rather they are shadowed is it ???
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:



Output


Question
Why output look like that ?


Answer
because s.greeting() :: s is a Super. (Compile Time Biding) and
s.name() :: s is Sub. (Runtime binding)

[ November 15, 2004: Message edited by: somkiat puisungnoen ]
[ November 15, 2004: Message edited by: somkiat puisungnoen ]
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Originally posted by Ilja Preuss
No, it cannot be overridden



I agree to it. I was wrong

Nikhil
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!