Jump to content

View more

Image of the Day

雑魚は多めにして、爽快感重視にしつつ・・・(´・ω・`)
早いとこ、ベースを作って、完成にもっていかないとね。
タイトルもまだ迷ってるだよなぁ。 
#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Up casting and ClassCastException

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 P0jahn   Members   

307
Like
0Likes
Like

Posted 04 May 2013 - 11:16 AM

Well, casting up and changing the object type is not possible in Java. So now I am kinda stuck.

 

I have a root class called GameObject. It contains a function that looks like this:

 

public GameObject getClone(float x, float y)
{
       GameObject go = new GameObject();
       //Clone the data here

       return go;
}

Then we have MovableObject which extends GameObject:

 

public MovableObject getClone(float x, float y)
{
       MovableObject mo = (MovableObject) super.getClone(x,y);
      //Clone the data here

       return mo;
} 

The cast operation will trigger ClassCastException.

 

Since a MovableObject have all the variables GameObject have + more, it would make sense to call the super class function first and then continue on the divided class function.

 

Are there any efficient ways to solve this?



#2 Bubsy   Members   

407
Like
0Likes
Like

Posted 04 May 2013 - 06:03 PM

Try this :

 

public class GameObject implements Cloneable {
  public GameObject getClone(float x, float y) {
    try {
      GameObject go = (GameObject)(super.clone());
      //Clone the data here
      return go;
    } catch (CloneNotSupportedException ex) {
      throw new RuntimeException(ex);
    }
  }
} 

 

P.S. I don't think that your clone method should receive any parameter. Try to keep the cohesion of the methods.



#3 zaphod2   Members   

1515
Like
0Likes
Like

Posted 05 May 2013 - 12:05 AM

The problem is that you actually creates a GameObject instead of a MovableObject. After an object is create it's impossible to change its runtime type. Casting is just for using different static type for this object. That means by casting you can refer the same object with different references. For example you can refer a MovableObject with a GameObject reference. Or if you get a GameObject reference but you know it points to a MovableObject, with casting you can refer to it as a MovableObject. But you can't refer a actual GameObject with a MoveableObject reference because it's not a MovableObject.

That's why you can't call the object creation code of the parent class from the child class. However you can call the data copying code if you separate it like this:

 

GameObject class:

 

public GameObject getClone()
{
    GameObject go = new GameObject();
    copyData(go);
    return go;
}

protected void copyData(GameObject other)
{
    other.someBasicData = someBasicData;
}

 

MovableObject class:

public MovableObject getClone(float x, float y)
{
    MovableObject mo = new MovableObject(x, y);
    copyData(mo);
    return mo;
}

protected void copyData(MovableObject other)
{
    super.copyData((GameObject)other);
    other.somethingNew = somethingNew;
}

 

 



#4 TheChubu   Members   

9323
Like
0Likes
Like

Posted 05 May 2013 - 12:43 AM

That's called downcasting btw. Because you cast an object of a higher hierarchy (GameObject) to a lower hierarchy (MovableObject) in your hierarchy tree.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.