aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why this error "casting "" Watch "why this error "casting "" New topic
Author

why this error "casting "

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
run this program and see and why the error
class Test
2: {
3: static void show()
4: {
5: System.out.println("Show method in Test class");
6: }
7: }
8:
9: public class Q2 extends Test
10: {
11: static void show()
12: {
13: System.out.println("Show method in Q2 class");
14: }
15: public static void main(String[] args)
16: {
17: Test t = new Test();
18: t.show();
19: Q2 q = new Q2();
20: q.show();
21:
22: t = q;
23: t.show();
24:
25: q = t;
26: q.show();
27: }
28: }


Harish<br />SCJP 1.4 (85%)
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Call static methods as
Test.show();

What you are trying to do has no sense with static methods.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
thats okay ,
but it gives explicit casting required at line 25 running this code
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Try to add t.getClass().getName(); an you will see what type is t or q. Generaly you cannot downcast.
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Harish-

It doesn't like the "q=t;" line because there is no guarantee that t is of a type that can be legitimately assigned to q. The extends clause makes guarantees about all Q2 objects being instances of Test objects, but none about Test objects being instances of Q2 objects.

For instance, let's say we had the following 3 classes:

Fruit
Pear extends Fruit
Apple extends Fruit


Example #1
These are ok because p1 and a1 are guaranteed to be fruit since both Pear and Apple extend Fruit.
Pear p1 = new Pear();
Apple a1 = new Apple();
Fruit f1;
f1 = p1;
f1 = a1;

Example #2
This is not ok:
Pear p2;
Fruit f2 = new Pear();
p2 = f2; // f2 might be a Pear, but might not.
In this case it was, but we could just as easily have change the previous line to be Fruit f2 = new Apple(); or Fruit f2 = getRandomFruit(); and then we would be assigning an Apple (or who knows what) to a Pear.


Example #3
This is ok, because of the explicit casting:
Pear p3;
Fruit f3 = new Pear();
p3 = (Pear)f3; // casted

Hope that helps.

Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
thanks Smith
for the example
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Static Methods doesnot participate in inheritance. What you are trying to do just shadowing the method of the base class.

Okay, Error is generating because when you're assigning a base class reference to the derived class reference variable, you're not casting it.
Try to do something like this at line 25:
--------------------------
q = (Derived)t;
q.show();
------------------------
Now it will run fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this error "casting "