• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
Priya Rajan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic