wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment" Watch "assignment" New topic
Author

assignment

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();
t.show();
Q2 q = new Q2();
q.show();
t = q;
t.show();
q = (Q2)t;
q.show();
}
}
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 t.show now mean q.show?


[This message has been edited by hema janani (edited August 24, 2000).]
Anonymous
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.
Vani
Krishna Shubha
Greenhorn

Joined: Dec 08, 2006
Posts: 18
Hi,
may be I am wrong for asking you this question at this time but I got confused .....in 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 that..here 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.
Krishna
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 http://www.javaranch.com/ubb/Forum24/HTML/002279.html
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
Greenhorn

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);
System.out.println((String)x);
"


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?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: assignment
 
Similar Threads
Majji exam1 Qno2
plz help
plz help
Tips,oneliner Just before the SCJP Exam
Sub Class and super class