Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with casting a child class into it's "grandparent" class

807580Oct 24 2009 — edited Oct 24 2009
Hey guys,

I'm currently wokring on a small game, for which i decided to create a tool.
The game is a 2D role playing game and uses a map.
The tool is supposed to make creating another map level easier.

In this project I have the following packages : MainPackage, Monster, TNPC, MapFields and MapObjects.
Monster contains some monster who all extends monster.class , which extends NPC
TNPC contains some talking npcs who all extends TNPC.class , which extends NPC

My problem :
I can't cast a monster or a TNPC into a NPC
eg :
  			if (data.equals("Phoenix"))
npcs[j] = new Phoenix(x, y, j);
does not work in the tool code

But :
1) In the game code, it works , altough I use the same code
2) I extend the map objects the same way (some mapObject extends MapObject.class extends MapField.class , but 
if (data[i].equals("LadderUp"))
Fields[x][y] = new LadderUp(x, y);
works

So :
does anyone have a solution (or idea) for me?

THX                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2009
Added on Oct 24 2009
5 comments
463 views