File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding 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 "overriding" Watch "overriding" New topic
Author

overriding

Priya Rajan
Greenhorn

Joined: Jul 06, 2001
Posts: 27
Friends,
static methods may not be overridden. true or false?
Complete Java Certification Guide by Roberts & heller says true in its mock exam provided in the CD.
class zzz{
static void print(){
System.out.println("hello");
}

}
class yyy extends zzz{
static zzz old=new zzz();
static void print(){
old.print();
System.out.println("world");
}
public static void main(String[] args) {
print();
}
}
The output prints hello followed by world. So I feel the answer is false in the question.

Thanks.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Static methods cannot be overriden.The reason you see "Hello" is because static methods are inherited in the subclass.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
Priya Rajan
Greenhorn

Joined: Jul 06, 2001
Posts: 27
Sandeep,
can u explain me more clearly. I could not understand.I am calling the super class static method inside subclass overridden method.
I read in a Cerification Guide by Robert Heller that
static methods may not be overridden by non-static. (it implies static methods may be overridden by static methods)
Thanks
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
I would like to give some of Jane Cristi, bartender's explanations (of course in some other thread of this forum) here
----------------------------------------
Hiding has to do with Class to Class access to members. If a member would have been inherited or accessible from a subclass, but that subclass declares a member with the same name (and in the case of methods, the same signature), the access to the superclass in interferred with, it is called hidden. As such overriding is a form of hiding.
Shadowing has to do with scope within a class. If a variable is declared as a member variable and then the SAME name is used in a local scope, the local variable shadows the member variable within that scope (like inside a method).

Obscuring has to do with namespaces. If the same name is used for a variable and for a class which is then used as the TYPE for a variable and for a package it becomes unclear which is intended when using that name. The rules are that in such obscure situations the system will make the presumption in the following order: variable->type->package.
--------------------------
Static methods are resolved at compile time and DO NOT participate in polymorphism (things like overriding). Only things resolved at runtime can do overriding.
If a super class and a sub class both have a static method with the same signature, they are both available to you at class load time (even before you make any instances of the classes).
SuperName.staticMethod();
SubName.staticMethod();
--------------
static methods are resolved at compile time, and do not therefore participate in polymorphism. Non-static methods, on the other hand, DO participate in polymorphism (through what is called late-binding.)

HTH

Priya Rajan
Greenhorn

Joined: Jul 06, 2001
Posts: 27
Thank u tvs. I have exam tomorrow. ur reply is very useful.
Originally posted by tvs sundaram:
I would like to give some of Jane Cristi, bartender's explanations (of course in some other thread of this forum) here

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Very good explanation from Jane.
To keep it simple : Static are compile time associated. Polymorphism are run-time associated.
So if we do try to create 2 static methods of same signature,
its nuthing but Having two methods of same signature... thats all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding