• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
michael D'souza
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
michael D'souza
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if the method show was not static.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i had a doubt
is there any significance effect of placing the Main class in parent or child class
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic