Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

class casting  RSS feed

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {

}

class B extends A {

}

class C extends A {

}

class D extends B {

}

public class Test {

public static void main(String args[]) {

A a=new A();
B b;
C c=new C();
D d=new D();
c=(C)d;//this is the line i get error
a=d;
b=d;
b=(B)a;
a=b;
}
}
/********************what is the reason and how can i correct it*******/
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting the exact text of the error messages helps a LOT. Those messages are chock-full of useful tidbits on what the problem is. without it, someone either has to guess what the real problem is, or cut-n-paste your code into an editor, save the file with the right name, then compile it.

many people won't go to that trouble.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inconvertible types


found: D
required : C
c=(C)d;
error

/********* this is the error**********/

thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eyeballing your code, i think the problem is that D isn't a child class of C. you have this kind of tree:



So, it doesn't make sense to cast a D to a C. you can't go up one branch and down another in your casts - only up or down a single branch.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick reply


but anyways if we want to get rid of the error at compile time?? what to do??
please let me know if it is possible?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only way to get rid of the error at compile time is to not make that cast in your code.

It is impossible for a object of type 'D' to pretend to be an object of type 'C'. remember that casting doesn't change the underlying object type, it just changes what the reference variable thinks the object is.

What would happen if in your class 'C', you had a method doCStuff()? the object of type 'D' wouldn't know anything about that method, but according to the rules of casting, you should be able to call it using your 'c' reference. This would cause Bad Things to happen during runtime.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Fred's little tree a lot. Let's look at what the compiler knows from that. First, looking at just one class:

The compiler knows this will work because nobody can call this with anything that isn't a B or a subclass of B.

The compiler knows this might work if the argument is a D, but not if it's a B. You're assuming all the risk and telling the compiler you know what you're doing when you do the cast, so the compiler lets you try it. The risk is that the argument is a B and you get a class cast exception at run time.

The compiler knows this will not work because a C is not a B. C is on a different branch of the tree. It also won't let anybody call the method with a C object. It knows enough to flag this at compile time instead of waiting for run time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!