• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Overriding static / non static methods

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across the following question:
Which of the following statements are true:
A. A static method may be overriden by a static method.
B. A static method may be overriden by a non-static method
C. A non-static method may be overriden by a static method
D. A non-static method may be overriden by a final non-static method.
The answer is D.
I agree D is true.
But since the only info I leaned about this from the book Im reading is that
"A static method may not be overriden to be non-static " , I could not understand why options
A and C are false.... ???
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static method can not take part in overriding. Overriding is for instance methods only. From the JLS, §8.4.6 Inheritance, Overriding, and Hiding:


A compile-time error occurs if an instance method overrides a static method.
...
A compile-time error occurs if a static method hides an instance method.


That's why those answers are incorrect.
I hope that helps,
Corey
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey.
The sad part is that the book Im reading to prepare for the test never mentioned that.
Also I was misled because a static method being overriden by a static method does not cause a compilation error...
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option A.
A static method may be overriden by a static method.
/*
There is only "hiding " of static method instead of overriding
*/
try it:


// the result is
This is A.m1()
This is B.m1()
If overriding occur, the result will be
This is B.m1();
This is B.m1();
Therefore, there will be no overriding of preceding code.
Pls noted that hiding of method means the which method is invoked is determined by the reference type but not the underlying object.
hope this can help
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but does that mean a static method can hide another static method. I guess my concern is about compiling. It sounds to me like it would compile, but with static methods, we are just using the wrong terminology, it is not being overriden it is being hidden. Is that correct?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wilson Mui:
but does that mean a static method can hide another static method. I guess my concern is about compiling. It sounds to me like it would compile, but with static methods, we are just using the wrong terminology, it is not being overriden it is being hidden. Is that correct?


Yes, that is correct. Overriding always implies polymorphism and static methods are not polymorphic.
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So calls to static methods will be determined at Compilation time just like it happens to overloaded methods ?
btw, that makes me wonder, any special comments about private methods ?
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I actually asked that couple of days ago. Private methods are in a sense, well are, hidden from any subclass. So if the subclass decides to declare a method with the same name, return type, and parameters, that is fine. It is not considered overloading. I believe even if you declare a method
private [static] final, it can still be redeclared in the subclass. Because the method is, in a very literal sense hidden. So the final modifier isn't violated, because it is not being overriden, and the static modifier isn't being violated (even if the subclass' redeclared method is not static) because it is hidden. Does that make sense.
Can somebody correct me if I'm wrong? Like I said, I just learned this a couple of days ago too.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wilson Mui:
Private methods are in a sense, well are, hidden from any subclass.


The Java Language Specification assigns a very specific meaning to the term "hidden" so it is better to avoid using it for anything beyond that specified by the JLS. It is more correct to say that a private method is not accessible.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic