This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment" Watch "assignment" New topic


hema janani
Ranch Hand

Joined: May 19, 2000
Posts: 70
This is from Majji -Q2
class Test
static void show() {
System.out.println("Show method in Test class");
public class Q2 extends Test
{ static void show()
System.out.println("Show method in Q2 class");
public static void main(String[] args)
Test t = new Test();;
Q2 q = new Q2();;
t = q;;
q = (Q2)t;;
ans is
C) prints "Show method in Test class"...1
"Show method in Q2 class".....2
"Show method in Test class"...3
"Show method in Q2 class".....4
shouldn't the ans be line 1,2,4,3 bcoz then what is the meaning of assigning t=q ?doesn't now mean

[This message has been edited by hema janani (edited August 24, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello Hema,
Remember that static methods are not overridden and hence there is no polymorphic behavior. If you use a reference to invoke a static method, the method corresponding to the type of the reference is called. In the code, using t ( type Test) calls the method in Test, using q(type Q2) calls the method in Q2 and so on.
Krishna Shubha

Joined: Dec 08, 2006
Posts: 18
may be I am wrong for asking you this question at this time but I got confused the code above why it's not giving any ClassCastException at q=(Q2)t....when I was experimenting such type of examples by taking one parent class and a subclass I am getting the error when I tried to do I am giving my simple example I tried...
class parent {}
class sub extends parent{}
public static void main(String args[])
parent p=new parent();
sub s= new sub();
p=s //compiles and runs fine
s=(sub)p //giving classcastexception at run time..
I think the same thing done in the above code..please let me know.
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
the point to notice here is not the hiding of static methods but the fact that
"When you pass an object reference variable to a function, you can potentially change the state of the object, but you can't make the passed reference variable refer to another object. "
as mentioned by DaB here
U can visit this post for in-depth understanding. Well it helped me get the logic and it works perfectly for the code given by Hema.
Baskaran Subramani
Ranch Hand

Joined: Jul 30, 2000
Posts: 38
I too have the same doubt as Krishna. I expected a class cast exception at runtime in
q = (Q2)t;
Since t is of type parent, how come it is not giving class cast exception at runtime?
Shankar G

Joined: Sep 24, 2000
Posts: 16
The above given code would not give ClassCastException when the Super class & Sub class are directly related -- The below statements from the Java API should clear your doubt:
"Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);

Originally posted by Baskaran Subramani:
I too have the same doubt as Krishna. I expected a class cast exception at runtime in
q = (Q2)t;
Since t is of type parent, how come it is not giving class cast exception at runtime?

Don't get me started about those stupid light bulbs.
subject: assignment
It's not a secret anymore!