aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods" Watch "Overriding static methods" New topic
Author

Overriding static methods

jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
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???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
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
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Dec 04, 2008
Posts: 49
[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 ]
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks..Harvinder. I missed the point that super is called from a non-static go() method.
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding static methods