• 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

Can static method be overridden?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jan

Its a challenging question.Anybody can answer it?
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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
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...
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone still unsure about whether static methods can be overridden (they cannot), please see Overriding vs. Hiding.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic