• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding static methods

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,
In the SCJP 6 exam preparation book it is said (page 107) that "you can't override a method marked static"

moreover, it is said on the next page "Using super to invoke an overriden method only applies to instance methods"

However, when I test the following code:



Not only does it compile (on Java 5), but I get the following result:


hepla
hopla
hoplaboum
hoplaboum



So is that something new that came with java 6, or did I misunderstand something from the book???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot override a static method, but that doesn't mean you'll get a compiler error when you create a static method in a subclass with the same name and arguments as a static method in the superclass.

What happens is that the static 'hopla' method in class Test hides (but doesn't override) the static 'hopla' method in class Foolaround.

The usual overriding rules do not apply to static methods. Try this, for example:

The output is:

Subclass.method()
Superclass.staticMethod()

Note that Superclass.staticMethod() is called, not Subclass.staticMethod() as happens with a non-static method.

Note also that you shouldn't call static methods from a non-static context; i.e., don't call a static method on an instance (as I'm doing here with obj.staticMethod()). Static methods are really class methods (they apply to the class, and not to a specific instance of the class). Calling a static method on an instance is confusing, because when you see such a call you might think that it applies to the object you're calling it on, while that isn't the case.

The question of whether you can override static methods has been discussed many times before in the forums here. Please do a search in the forums (click "Search" in the top right of the page) and you'll find more answers.
[ December 14, 2008: Message edited by: Jesper Young ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the SCJP 6 exam preparation book it is said (page 107) that "you can't override a method marked static"



It's a distinction on what it means to override a method. Having a method with the same name and signature in a subclass, is *not* overriding if the method is static. This is because it is still possible to get to the base class method, by casting the reference, or by specifying the base class. It is more hiding the method than overriding the method.

So is that something new that came with java 6, or did I misunderstand something from the book???



Basically, with your example, you showned that you can hide a static method, with a method that you invoked. And you showned that you can also get to the hidden method, using a super call from an instance method. It doesn't prove that you can override a static method.

Henry
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks for those details.

My question mainly came from a question in the Learnkey master exam:



The correct answer according to the exam is:

C. Does not compile due to an error in line #1

to which I agree, but the explanation is:

C is correct. Only instance methods can be overridden, and calls to super only apply to overridden methods.

and there I didn't agree, because IMO the problem is simply that super cannot be invoked from a static context.

Hence this got me all messed up
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to clarify the following from the 2 examples provided by jean-gobert de coster .
In the code:
class Foolaround {
public static void hopla(){
System.out.println("hopla");
}
public void hepla(){
System.out.println("hepla");
}
}

class Test extends Foolaround{
public static void main(String[] args) {
new Test().go();
}
public void go() {
super.hepla();
super.hopla();
hopla();
Test.hopla();
}
public static void hopla() {
System.out.println("hoplaboum");
}
}
super.hopla(); give NO compiler error.
However in
class AlternateFuel {
int getRating() { return 42; }
static int getRating2() { return 43; }
}
class BioDiesel extends AlternateFuel {
public static void main(String[] args) {
new BioDiesel().go();
System.out.print(super.getRating2()); // #1
}
void go() {
System.out.print(super.getRating()); // #2
}
}
super.getRating2() gives compilation error. It seems to me in both cases you are trying to access the static method of the superclass. While I think understand why super.getRating2() gives compilation error (You cannot invoke a static method using an instance as it belongs to the class. Then again if you invoke it as getRating2() you are implicitly using "this", and you do not get any compilation error.)
I don't understand why we are not getting the compilation error. Can any of you please explain?

Thanks,
Meera
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Using super to invoke an overriden method only applies to instance methods




But in this case we are using super.hopla to invoke a static method, why is it giving the correct answer then?
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[deleted some uninteresting part]
super.doSomething(); won't work because "super" is not a static call.

think about it, would you do this.doSomething() in the main method? What would "this" refer to?
[ December 17, 2008: Message edited by: jean-gobert de coster ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meera:

You cannot invoke a static method using an instance as it belongs to the class.



Who said that?



Output:
Parent Static Method

I'm invoking the static method of the Parent class using the instance of the Child class.

I could also replace line#1 with the following statement:
new Parent().Method();
and get the same output.

Moreover, I could also replace line#1 with the following statement:
Method();
and get the same output because all non-private methods including abstract methods if any are inherited by the subclass.

If i uncomment the commented code and comment line#1 in the Child class then i am hiding the static Method() method in the superclass Parent.
But i can still invoke the static Method() method of Parent Class from Child class using:
super.Method(); from the *NON-STATIC* go() method even though Method() method is itself static.

super or this *CANNOT* be invoked from a static context.

Originally posted by Abhi:
But in this case we are using super.hopla to invoke a static method, why is it giving the correct answer then?




Quoting the JLS, "A hidden method can be accessed by using a qualified name or by using a method invocation expression (�15.12) that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields"



Hope it resolves your doubts.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..Harvinder. I missed the point that super is called from a non-static go() method.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class AlternateFuel {
int getRating() { return 42; }
static int getRating2() { return 43; }
// static int getRating() { return 43; }
}
public class BioDiesel extends AlternateFuel {
public static void main(String[] args) {
new BioDiesel().go();
// System.out.print(super.getRating2());
// #1 Compiler error because trying to access a static method using super
System.out.print(getRating2());
}
void go() {
System.out.print(super.getRating()); // #2
System.out.print(getRating());
System.out.print(super.getRating2());
}
}

So in the above code the reason super.getRating2() did not work is because it was invoked at Line#1 using super since super or this cannot be used in static context.
Thanks,
Meera
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic