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 Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic
Author

Question

michael D'souza
Greenhorn

Joined: Apr 14, 2004
Posts: 5
Here is a java code

class Test1
{
static void show()
{
System.out.println("Show method in Test class");
}
}

public class Q2 extends Test1
{
static void show()
{
System.out.println("Show method in Q2 class");
}
public static void main(String[] args)
{
Test1 t = new Test1();
t.show();
Q2 q = new Q2();
q.show();

t = q;
t.show();

q = (Q2)t;
q.show();
}
}

The answer is:
Show method in Test class
Show method in Q2 class
Show method in Test class
Show method in Q2 class


Can some one explain me how the last 2 outputs are there.
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 90
    
    5

The answer is:
Show method in Test class
Show method in Q2 class
Show method in Test class
Show method in Q2 class

Can some one explain me how the last 2 outputs are there.


For static function, the reference value doesn't matter at all. Only the type refers. So, even if you have:


than it will execute the method from Test1 class.
Also, keep in mind that static functions are not "virtual" functions, therefor even if t is-a Q1, then it will call method from Test1.


Adam
SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"michael" please take the time to read our JavaRanch Naming Policy and change your displayed name to comply with it. We require a first name and a family name preferably your real names.
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
michael D'souza
Greenhorn

Joined: Apr 14, 2004
Posts: 5
what if the method show was not static.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i just added some extra code to the original question,but it is not working it throwing run time exception classcastexception

class Test1
{
static void show()
{
System.out.println("Show method in Test class");
}
}

class Q2 extends Test1
{
static void show()
{
System.out.println("Show method in Q2 class");
}
public static void main(String[] args)
{
Test1 t = new Test1();
t.show();

Q2 q = new Q2();
q.show();

t = q;
t.show();

q = (Q2)t;
q.show();


//Here's the code i added

Test1 t1 = new Test1();
//t1.show();

Q2 q1 = new Q2();
//q1.show();

t1 = q1;
t1.show();

Test1 t12=new Test1();

q1 = (Q2)t12; //throwing error saying the classcast exception how
//q = (Q2)t; is working fine.
q1.show();




}
}



clear my doubt please
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

Test1 t12=new Test1();

q1 = (Q2)t12; //throwing error saying the classcast exception how
//q = (Q2)t; is working fine.
q1.show();



Parameswaran,
U have created t12 reference to an object of class Test1.
Test1 t12=new Test1();
Q2 q1 = new Q2();
q1 = (Q2)t12;

what u have done is created an object of Q2 and assigning Test object to Q2, which is obviously not possible.(as Test is the parent class for Q2)

But in the second case u have:

Here before doing "q = (Q2)t;" u see, first Q2 reference q is assigned to Test1's reference. Which means t now belongs to Q2 class. So, "q = Q2)t" would not result in any class cast exception.

Hope u got it
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
can i say that before casting any reference variable to a particular type ,we need to make the reference variable to point to the object or reference of that particular type.

eg:

refvar1=Type object or Type refvar

Type refvar=(Type)ref var1

if it is so is this common ground rule.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

can i say that before casting any reference variable to a particular type ,we need to make the reference variable to point to the object or reference of that particular type.

yes this is what i meant, and this is quite logical, isn't it. Think logically not in terms of java.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
class derSimple extends Simple
{
int x;
public void show()
{
System.out.println("this is in derived class");
}
}
public class Simple
{
public void show()
{
System.out.println("this is in derived class");
}
public static void main(String[] args)
{
Simple ob = new Simple();
Simple refob;
derSimple dob = new derSimple();
derSimple refdob;
//ob = dob;
dob = (derSimple)ob;
dob.show();
}
}

here if i remove the comments then it is working fine, otherwise it is giving classcast exception.
even in the previous example also that is the case.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hey i had a doubt
is there any significance effect of placing the Main class in parent or child class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question