File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "static methods" Watch "static methods" New topic
Author

static methods

rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
can the static methods be overridden?if so, under which conditions?
huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68

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


SCJP
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
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.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Mar 10, 2001
Posts: 151
thanx Bill for the explanation.it has cleared my doubt.
Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
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


ANaveenS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods