tags:

views:

163

answers:

8

I have some code in Java as follows:

private Object addVertex(String label, int posX, int posY) {
 Vertex newVertex = new Vertex();
 this.getModel().beginUpdate();
 try {
  newVertex = insertVertex(parent, null, label, posX, posY, 80, 30);
 }
 finally {
  this.getModel().endUpdate();
 }
 return newVertex;
}

That code wont work because of a type mismatch, insertVertex will return an Object, but I get a type mismatch since it can't convert from Object to Vertex (which is an object I created).

Firstly, how come that can't work, since the object Vertex inherits Object by default surely you should be able to do that.

Also, if I try and type cast the Object to a Vertex as follows

newVertex = (Vertex) insertVertex(parent, null, label, posX, posY, 80, 30);

I get an error saying I can't make that conversion.

+1  A: 

Try to use

Object insertResult = insertVertex(parent, null, label, posX, posY, 80, 30);
if(insertResult instanceof Vertex){
   newVertex = (Vertex)insertResult;
}

If you get the default implementation the returned type is not a Vertex object.

The insertVertex method has to return an Object which extends from Vertex!

Markus Lausberg
+3  A: 

Vertex inherits from Object, but not the other way round. Basically you're trying to do:

Object tmp = insertVertex(...);
newVertex = tmp;

You can't assign an Object reference to newVertex because the latter is of type Vertex.

Now, with your cast it should be okay, so long as insertVertex is genuinely returning a Vertex at execution time. Please give details of exactly what error message you're getting.

Jon Skeet
+1  A: 

You should be able to cast the result of insertVertex to whatever you want. It may fail at runtime, but if you know that it will always be a Vertex, then it should not fail. As Markus suggests, you can use instanceof to test that.

As an aside, the code looks strange in the you create a new Vertex and assign it to the newVertex variable, but then don't pass that reference anywhere?

jabley
Actually, even the compiler will reject casts that it knows cannot succeed because the type that's being cast is from a different inheritance hierarchy than the type it's being cast to. The compiler error is "Cannot cast X to Y" in that case - perhaps this is what saralk is encountering.
Michael Borgwardt
+1  A: 

You cannot convert Object into an instance of Vertex, because Object is a superclass of Vertex, not the other way around.

Can you modify InsertVertex to return a Vertex object instead of an Object? There's really no reason not to. Otherwise, you will have to manually cast the Object to the Vertex object through a function.

futureelite7
+1  A: 

You can't assign a Fruit to a Banana. The Banana is more specific than a Fruit, it's a subclass of Fruit. You can do the opposite, Fruit f = new Banana();

This is what is happening here. If your insertVertex returns an Object it can only be assigned to another Object. If you cast it to Vertex and you get an error, then your insertVertex method must be declaring another type of Object that is not a subclass of Vertex.

bruno conde
A: 

Well for the definition of the function looks that what you want is to return Object, so probably you dont need to cast to Vertex and just define newVertex as an Object. However it looks that in any case you will need to do the casting in another function. It sounds to me that the cast should work and if not, please send the exceptions that have been generating.

4NDR01D3
+1  A: 

Are using any IDE to develop the code?

If the IDE allows you to debug the code by putting break points, you can put a break point in insertVertex() and check exactly what instance it is returning.

As Markus said you can use the instance of operator to check whether the value returned by insertVertex() is of type Vertex.

If you are getting a ClassCast Exception while assigning the return value of insertVertex() to variable newVertex, it is almost clear that the insertVertex() is not returning an instance of Vertex or any instance which are a subtype of Vertex.

If you don't have a debugger you can try something like this.

Object insertResult = insertVertex(parent, null, label, posX, posY, 80, 30);
System.out.println(insertResult);

This will print the type and object id in the console if the returned type is not overriding the toString() method.

Arun P Johny
A: 

There is no need to create a new Vertex just to initialize the variable.

    Vertex newVertex;

if you don't use the newVertex in the method, you can declare it as being an Object.

    Object newVertex;

or just return the result (if any) from insertVertex

private Object addVertex(String label, int posX, int posY) {
    Vertex newVertex = new Vertex();
    this.getModel().beginUpdate();
    try {
            return insertVertex(parent, null, label, posX, posY, 80, 30);
    }
    finally {
            this.getModel().endUpdate();
    }
}

[]]

Carlos Heuberger