• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this error "casting "

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: }
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call static methods as
Test.show();

What you are trying to do has no sense with static methods.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats okay ,
but it gives explicit casting required at line 25 running this code
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to add t.getClass().getName(); an you will see what type is t or q. Generaly you cannot downcast.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Smith
for the example
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic