aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods in inheritance 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 "static methods in inheritance" Watch "static methods in inheritance" New topic
Author

static methods in inheritance

Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
why i am getting below error when i compile the below program ? i thought static methods are hidden and cannot be overriden .

StaticTest.java:9: print() in StaticTest cannot override print() in StTest; atte
mpting to assign weaker access privileges; was protected
private static String print() {

Sources: just the test class i wrote ..


Thanks,
Suri
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

private method cant inherited by subclass, so there wont be a problem in your example.

however consider below case:


If you are declaring a method signature similar to its super class method in subclass
then you cant reduce the modifier visibilities irrespective of hiding or overriding
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

you are right that static methods CANNOT be overridden. they can be hidden in subclass. the error you encountered gives impression that static methods can be overridden(you get the same error when the access modifier of overriding method is more restrictive than method being overridden), but as Seetharaman said that irrespective of hiding or overriding the access modifiers CANNOT be more restrictive when you are overridding or when method hiding takes place. if you are still in doubt feel free to post


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
Thanks guys. Error message is definetely misleading though, I guess one more point to remember.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Seetharaman Venkatasamy wrote:


Actually this done not error out. Here the overriding method(print() in StaticTest) is not reducing the visibility(in fact it is increasing it) of the overridden method(print() in StTest).
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Praveen Kumar M K wrote:
Seetharaman Venkatasamy wrote:


i

Actually this done not error out. Here the overriding method(print() in StaticTest) is not reducing the visibility(in fact it is increasing it) of the overridden method(print() in StTest).


i think he meant private in subclass.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ah, my mistake - I misread StTest as a subclass of StaticTest ;) .
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
wow , good catch praveen .. now this would make a good question for exam :-) .. so i guess my question still remains ?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Surender Suri wrote:wow , good catch praveen .. now this would make a good question for exam :-) .. so i guess my question still remains ?

The error message given by the Eclipse compiler is a little more helpful:
Cannot reduce the visibility of the inherited method from StTest

The method print in StTest has protected access. The subclass StaticTest is trying to make it private. You can make inherited things more visible, but not less. I guess that's what Javac means by the phrase "weaker access privileges", but I agree with you that it is very unclear phrasing.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Surender Suri wrote:wow , good catch praveen .. now this would make a good question for exam :-) .. so i guess my question still remains ?


Hello Surender,

Actually I dont think my observation clears any part of your original post. I just made that correction although I knew Seetha didnt mean to make that mistake, so that future readers who read the post out of context, get the right example.

As for your post, in my opinion its just a way of looking at the error message. As Gurupreet pointed out, it just qives you an impression, but if you look closely at the error message, you will see that the method being static has nothing to do with the error. You will see the same error message irrespective of method being static or not because it has to do with access specification only.

Refering to section 8.4 of JLS 7, you can see that there is no reference to static.

The access modifier (§6.6) of an overriding or hiding method must provide at least
as much access as the overridden or hidden method, as follows:

• If the overridden or hidden method is public, then the overriding or hiding
method must be public; otherwise, a compile-time error occurs.

• If the overridden or hidden method is protected, then the overriding or hiding
method must be protected or public; otherwise, a compile-time error occurs.

• If the overridden or hidden method has default (package) access, then the
overriding or hiding method must not be private; otherwise, a compile-time
error occurs.

Regards,
Praveen.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static methods in inheritance
 
Similar Threads
Static variable initialization
Looking for explanation
constructor and main whichone will execute first..
Just a doubt on static methods
Order of Static initialization Block,Instance initialization Block and Constructor....