• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Casting  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could someone please help me with the below ClassCastException ? Also what are the rules for classcasting.

class GameShape
{
}
class PlayerPiece extends GameShape
{
}
class TilePiece extends GameShape
{
}
public class TestShapes {

public static void main ( String args[])
{
GameShape game = new GameShape();
PlayerPiece player = new PlayerPiece();
TilePiece tile = new TilePiece();
doShapes((TilePiece) game); // Y DOES IT GIVE ME CLASSCAST EXCEPTION
}

public static void doShapes(TilePiece tile)
{
System.out.println("inside doShapes() method");

}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose,

A cast can never change an object into something it's not; it can only tell the compiler what kind of object a variable is really pointing to. So on this line

doShapes((TilePiece) game);

the variable "game" is pointing to an actual GameShape object, but you're telling the compiler that "game" actually points to a TileShape object. The compiler takes your word for it, but at runtime, Java checks, and find that ooops, it's not a TileShape object, so you get a ClassCastException.

Casts are useful when you have a variable of a more general type pointing to an object of a more specific type; so, for example

GameShape game = new TilePiece();
doShapes((TilePiece) game); // This cast is legal, because it's telling the truth.

The rules for casts are really simple: you have to tell the truth. That's it.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply it helped.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Make difference between object & reference.Object denote memory in heap while reference denote that heap space OR object.
2. So when you are casting you are making change in reference NOT in object.
2. Casting works between two classes if they have some super sub relationship.
3. A subclass object can be cast into superclass reference BUT opposite is not true.
For E.g.

class A {
}
class B extends A{
}
class C{}

class Check{
public static void main(String args[]){

A a= new A();
B b= new B();
C c= new C();

a=b //possible because a is super class reference variable

b=a // NOT possible because b is subclass reference variable

// these are NOT possible as there is no relationship between c & a,b
c=a
c=b;
a=c
a=b
}
}
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pankaj for your response. So in this case i can say :
a=b is upcasting or implicit casting.

How can I achieve downcasting in this program ?

Since we have a superclass - sublcass relationship, i hope we can do downcasting as well?

Also in the 4th statement :

"A subclass object can be cast into superclass reference BUT opposite is not true. " instead of "object" we should have "reference variable" ? Am just trying to understand difference between reference variable and object when it comes to casting. Thanks for your help.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose casting happens between reference varaibles. downcasting means that you are assigning superclass reference variable into subclass reference variable where superclass reference variable denoting subclass object.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pankak Vijay,

In the example that you gave, If I have :

A a= new B();
B b1= new B();

b1 = a; (Would this give me an error ???)


Also i would like to know clearly in the code, whats meant by reference variable and object. Is it only a memory concept ?? For example in the above code sample a is a reference or an object or both ???

Trying to understand refrence variable and object so that the rules for casting become simpler to understand. Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:b1 = a; (Would this give me an error ???)

Why don't you try it?
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i tried and it naturally gave an error...since the cast was not impilcitly mentioned!!

However once we mention the cast it works perfectly fine :




This means "You can cast down the hierarchy as well but you must explicitly write the cast and the object must be a
legitimate instance of the class you are casting to."
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote: . . . "You can cast down the hierarchy as well but you must explicitly write the cast and the object must be a
legitimate instance of the class you are casting to."
There was a similar question recently: here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!