• 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

override static method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Marcus Green Exam No1, nr 16, static methods cannot be overridden. Is that correct? If so why does the following compile?
class Parent{
static void foo(){}
}
class Child extends Parent{
static void foo(){}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static method cannot be overridden but they may be hidden. The key point is the overriding applies to non-static method and hiding to static ones.
You cannot override a static method with a non-static method but you may hide a static method with another static method
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can think of static method like private method.
They are "static binding" at complier time.

[This message has been edited by FEI NG (edited November 18, 2001).]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
static methods are not over loaded by not static methods and vice versa but in static methods late binding is done.i.e method is called depending upon the reference not object?
Correct me if i am wrong
Thanks

------------------
Regards
Farrukh Mahmud
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farrukh
I do not think that static methods are late binded...
as at compile time reference variable type is known.
CMIW
------------------
Regards
Ravish
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well look the code

if static methods are not late binded than how it will be know that parent dostuff is called?
plz clear my this confusion
Thanks

------------------
Regards
Farrukh Mahmud
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi farrukh
as p is refernce type of class Parent , it will always call Parent's doStuff() method as this method is static. It does not matter whether *p* is holding object of Child or GrandChild.
It has been checked at the compile time that method is static and hence it is early binded to the method of class Parent as p is reference type of class *Parent*.
And always remeber *static* is relative to Class , not to object.
HTH
------------------
Regards
Ravish
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravish kumar:
Hi Farrukh
I do not think that static methods are late binded...
as at compile time reference variable type is known.
CMIW


agree with ravish,
static methods are not late binded
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Valentin Crettaz :
You said :
"static method cannot be overridden but they may be hidden. The key point is the overriding applies to non-static method and hiding to static ones.
You cannot override a static method with a non-static method but you may hide a static method with another static method "
and I agree with this
but clarify the next point :
import java.io.*;
class Parents{
static void foo(){}
}
class Childs extends Parents{
public static void main(String args[]){

}
static void foo() throws IOException{}
}
gives the error message :
The exception "IOException" is not the same as or a subclass of any exception in the throws clause of the overridden method "void foo();" declared in type "Parents".
From what I see in the error message dumped, the static method void foo() in subclass can't twrow an exception not thrown by the base class Parents (obvious an overriding feature).What about that hiding stuff ?
(we shouldn't care about exception thrown by the base class static void foo() as long as all is about hiding not overriding)
Thanks in advance
Best regards
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by farrukh mahmud:
hi
static methods are not over loaded by not static methods and vice versa but in static methods late binding is done.i.e method is called depending upon the reference not object?
Correct me if i am wrong
Thanks


Hi Farrukh, the static methods can be overloded with non-static method, I think u mean to write overriden.


------------------
Muhammad Farooq
Sun Certified Programmer for Java 2 Platform
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Salamina:
About your question, the JLS (8.4.6.3) provides the answer:


If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return
type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides or hides; otherwise, a compile-time error occurs.


I think that's pretty clear, isn't it ?
Let me know if not...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Salamina Daniel
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed your JLS excerpt:
"method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides or hides;"
makes everything crystal clear now.
Thanks for your explanation!
Best regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic