aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can static method be overridden? 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 "Can static method be overridden?" Watch "Can static method be overridden?" New topic
Author

Can static method be overridden?

Jan Nowak
Greenhorn

Joined: Aug 12, 2007
Posts: 16
Hello everyone,
I am new to this forum. Recently I decided to prepare for SCJP 1.5. As a result I have questions and sometimes I will ask you guys for help when I won't be able to understand something.

I have read in K&B that static methods can't be overridden, only redefined. But when I do something like this:

import java.io.*
class A{
static void fun(int a){}
}

class B extends A{
static void fun(int a) throws IOException{}
}

The compiler says that "fun() in B cannot override fun() in A; overridden method does not throw java.io.IOException."
I get an error when I try to change return type as well or define it as nonstatic method.

It seems that rules for overriding nonstatic methods apply to static methods as well. I know that static methods can't participate in polymorphism.

So, can static methods be overridden or not? K&B say they cannot but compiler says "cannot override fun()". Why overriding rules apply to static methods? How to answer an exam questions if I encounter such?

And second question: Consider this:

double d = 10.5;
byte b = 0;

b = d; //compilation error because narrowing conversion
b += d; // OK!!! b is now 10!!! implicit conversion here?

Why does the second line compile and run fine?
[ August 12, 2007: Message edited by: Jan Nowak ]
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
Hi Jan Nowak

I am not sure with the first question,but I know they can be overridden.
For the second question,
All compound operators like +=,-=,*=,/=,%= use automatic casting.
They take care of the implicit casting.

Thanks
Praveen SP
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Hi Jan

Its a challenging question.Anybody can answer it?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

What is polymorphism ?
How do you achieve in java ?

After preparing for the above then ask your self this question

Does static behavior can participate in polymorphism ?

you will get the answer.

Some times you need to come from the beginning if we forget that.
otherwise the imaginary building collapses.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
After reading your post again I thought I might have hurt you int my previous mail. sorry for that.

static methods can't participate in polymorphism.
1. Overloading
2. Overriding.

CASE #1. Overriding. How this guy gonna prove ? lets see by example.



try this out.

Overloading next post

Why you are getting overriding sysntacx error by the compiler ?

Here in case of inheritance. The child is allowed to block static behavior coming from parent.

We call it as Hiding in static context.

Compiler designers levied the same rules for blocking the parent static behavior as they did for over-riding checks.

Thats why you got those confusing words from compiler.
[ August 13, 2007: Message edited by: Srinivasan thoyyeti ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

About the static method: The error message that the compiler gives you is indeed strange, because static methods cannot be overridden.

According to the Java Language Specification section 8.4.8, a static method can hide (but not override) other static methods in superclasses.

The fact that you get an error is correct; in section 8.4.8.3 the JLS says:

"A method declaration must not have a throws clause that conflicts (�8.4.6) with that of any method that it overrides or hides; otherwise, a compile-time error occurs."

It's just the wording of the error message from the compiler that's incorrect - it should have said "fun() in B cannot hide fun() in A" instead of "override".

The Java compiler isn't perfect, it's also just a piece of software written by a human being...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
Hi Srinivasan,
The output for your code is:
Parent :: static method
Parent :: static method
Parent :: static method //3
Child :: static method
Child :: static method
Child :: static method

i didn't get any compiler error.
Also i have a doubt that the output for this piece of code in your program
given below is different from what i have expected.

Parent parent = new Child();
parent.staticMethod();

At runtime selection of staticMethod() is made here in overriding. The staticMethod() of Child class should have been called with output at //3 as:
Child :: static method
But this is not the case. Please help me to know about this.

Regards,
Seema
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Seema,

I thought you would have understood by seeing it behavior.

Any way static behavior(methods) will not participate in polymorphism.

Parent parent = null;

parent.staticMethod(); // it will work

What did you understand from that ?

To invoke a static method its sufficient to know Class to which the staticMethod belongs.

by seeing
parent reference it has understood that its of Type Parent class.
So it's able to invoke that.

it will not bother about Object what parent is pointing.
thats why null will not bother the jvm.

Having said that,

Parent p = new Child();
p.staticMethod(); // This should n't be a problem for you to understand this Now . Am i correct.
Got me. ?
[ August 13, 2007: Message edited by: Srinivasan thoyyeti ]
Jan Nowak
Greenhorn

Joined: Aug 12, 2007
Posts: 16
Hello,

Thanks all of you guys for your responses. So static methods can't be overridden but they can be hidden and rules for hiding static methods are the same like for overridding methods. I got it now. The compiler message confused me but now I understand, thanks.
Vikrant Sahdev
Ranch Hand

Joined: May 31, 2007
Posts: 58
The answer is just 2 lines :
Any overriding method cant throw an exception that is more specific than defined in the parent class. If there is no exception in parent class, then you cant add any at all.

Thats why you are getting compiler error.


Coming soon... www.javatales.com
Wayne Pegg
Greenhorn

Joined: Jul 09, 2006
Posts: 4
Hi there

I am preparing for the SCJP exam as well!

Hope this helps. Firstly static methods cannot be overridden as there is only one copy that belongs to the class. The evident difference between instance methods and static. As there is only one copy of a static method it cannot be overidden be an extending class.

Normal overridding rules dictate that new or broader exception cannot be defined in an overridding methods. You can declare the same or more specific exceptions.

In terms of the second part as d is declared type double the operation b += d; is implicitly cast to a double.

Hope this helps, if incorrect can someone please inform me please??

Wayne
Vikrant Sahdev
Ranch Hand

Joined: May 31, 2007
Posts: 58
Just wanted to add to what I said earlier :
Any overriding method can throw an exception that is more specific than defined in the parent class,not more general. If there is no exception in parent class, then you cant add any at all.

As far static methods overriding is concerend, it is essentially hiding.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Vikrant,

Wanna add one more point: RuntimeExceptions are not checked. we can decalre a method throw Any RuntimeException irrespective of parent throwing it or not.
[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]
Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
Originally posted by Jesper Young:
About the static method: The error message that the compiler gives you is indeed strange, because static methods cannot be overridden.

According to the Java Language Specification section 8.4.8, a static method can hide (but not override) other static methods in superclasses.

The fact that you get an error is correct; in section 8.4.8.3 the JLS says:

"A method declaration must not have a throws clause that conflicts (�8.4.6) with that of any method that it overrides or hides; otherwise, a compile-time error occurs."

It's just the wording of the error message from the compiler that's incorrect - it should have said "fun() in B cannot hide fun() in A" instead of "override".

The Java compiler isn't perfect, it's also just a piece of software written by a human being...


K&B use the word redefine instead of hide. I like it better.
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
hi there..


static methods cannot be overridden but they can be overloaded...

only inherited methods are overridden

and static methods are not inherited... they're private property of the class in which they are declared.

so they are not inherited by sub classes. hence, cannot be overridden...


Sandeep Atluri
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi sandeep,

Through Inderitance Child can acquire static and non-static behavior from Parent.

If we want to block Parent behavior in Child

1. In non-static context: we do over-riding
2. In static context : we do hiding.

For over-riding and hiding syntax rules same.

by over-riding we can have polymorphic calls like this
Parent parent = new Child();
parent.overriddenMethod();


and
by hiding we can just block Parent methods to have our own child implementation. ( No polymorphic calls.)
(see my example posted above)



This code is for understanding static method inheritance:


Please correct you post.
Vikrant Sahdev
Ranch Hand

Joined: May 31, 2007
Posts: 58
Originally posted by Srinivasan thoyyeti:
Hi Vikrant,

Wanna add one more point: RuntimeExceptions are not checked. we can decalre a method throw Any RuntimeException irrespective of parent throwing it or not.

[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]


RuntimeExceptions are unchecked/implicit so adding them or specifying them doesnt take anything away.So there is no point in worry about a subclass adding some unchecked/implicit exceptions in an overriding method.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Anyone still unsure about whether static methods can be overridden (they cannot), please see Overriding vs. Hiding.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks a lot Marc

the link shown by you Overriding vs hiding was superb!!!

I realy got to learn a lot!!!






Preparing SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can static method be overridden?