• 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

static methods

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can the static methods be overridden?if so, under which conditions?
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No overriding is only for instance method, however static method can be hidden. For further reference check JLS 8.6
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#228745

 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx huiying li for the link.But i am still unable to understand the concept of hiding by class methods(static methods)
could u simplify it for me?one more doubt.in one of the mock exams,it was asked if static methods can be overridden?the answer to it was no.but can't u override them by static methods themselves if not by instance methods.pls clear this doubt if possible.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is "no", static method cannot be overridden. If you try to override a static method with a non-static method, you will get a compile error, if you try to override a static method with another static method, you won't get an error, but it is not overriding, it is hiding.
When you override a method in a subclass, if you were to create an instance of the subclass and then upcast it to the parent like this:
Parent p = new Sub();
With overridden methods, the method of the subclass would be called. With hidden method, the method of the parent class would be called. That is the main difference. You don't get the late-binding that you do when you override non-static methods, hence it is not considered overriding.
Hope that helps,
Bill
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Bill for the explanation.it has cleared my doubt.
 
Greenhorn
Posts: 23
  • 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 overriden
* static methods are hided
instead of putting my own code, i leave it to sun & hope u'll get it!
----------------fromSun---------------------------------------
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of
the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke,
whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.

--------------------------------------------------------------
Rgds,
ANaveenS
 
reply
    Bookmark Topic Watch Topic
  • New Topic