17

The below code gives me the error:

SceneNode.java:17: cannot find symbol
symbol  : method execute() location:
class java.lang.Object
                operation.execute();
                         ^ 1 error

Code:

import java.util.LinkedList;
import java.util.Iterator;

public class SceneNode<T>{
    T operation;    
    public SceneNode() {
    }   
    public SceneNode(T operation) {
        this.operation = operation;
    }
    public void setOperation(T operation) {
        this.operation = operation;
    }
    public void doOperation() {
        operation.execute();
    }
}

It's a cut down (for your readability) start of a simple scene graph. The node could be a model, transformation, switch, etc., so I made a variable called operation that's type is defined by the T class variables. This way I can pass a Transformation / Model / Switch object (that has an execute method) and pass it like this:

SceneNode<Transformation> = new SceneNode<Transformation>(myTransformation);

I'm pretty sure having a base class of SceneNode and subclassing for all the various types of nodes would be a better idea (I was trying out generics, only learnt about them recently). Why doesn't this work? I must be missing something fundamental about generics.

4 Answers 4

13

It doesn't work because T could be any type, and Java is statically typed. The compiler has no idea whether you'll try to create a SceneNode<String> - then what would execute do?

One option is to create an appropriate interface, e.g.

public interface Executable {
    void execute();
}

and then to constrain T in SceneNode to implement Executable:

public class SceneNode<T extends Executable> {
    ...
}

(I find it a little bit odd that T has to extend Executable rather than implement it in the source code, but then T could end up being an interface itself, so I guess it makes sense.)

Then it should work fine. Of course you could make Executable an abstract superclass instead - or even a (non-final) concrete class - if you wanted, but I would generally prefer to use an interface unless I had some reason not to.

3
  • @dty: Yup, I was already on it. Looks weird to me, but such is Java generics :(
    – Jon Skeet
    Feb 26, 2011 at 21:01
  • sorry, but why is it not implements Executable?
    – Luk Aron
    Oct 29, 2019 at 10:09
  • @LukAron: Because that's just not the syntax used for generic type bounds. See docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.4. I understand it's a little odd, but it does make it consistent.
    – Jon Skeet
    Oct 29, 2019 at 10:10
10

I'm guessing you come from a C++ background.

The compiler has no idea what kind of a thing T might be because you haven't told it.

If you had an interface called, for example, Executable which defines your execute() method, then you would need to do:

public class SceneNode<T extends Executable> {
    // ... 
}

Now, the compiler will know that T is an Executable, and will give you access to all the methods on that interface.

4
  • 1
    Because a generic <T> is actually an Object for Java (is the same as <T extends Object>), until a base class is specified <T extends BaseClass> Feb 26, 2011 at 21:00
  • Thankyou, that's solved it and I now understand why as well. I had seen that syntax before but i thought it meant "object must be a subclass of this object". Which seems silly in hindsight.
    – Callum
    Feb 26, 2011 at 21:11
  • @Callum: It means "the parameter type must be a subtype of the given type", thus you now can only use subtypes of Executable for T. Feb 26, 2011 at 22:32
  • I come from c++ and actually find it hard to get too, so thanks for this explanation. It's a bit weird though, I would guess that the compiler should look for usages of a generic class to see if a method called is actually there - that's how it's done in c++. Having to specify a class that T extends from kind of diminishes usability of a generic class, it's not generic anymore - it's only for the objects extending this class, right? Anyway, I think I need to study this topic a bit more.
    – Konrad
    Dec 1, 2020 at 16:49
4

Recently I came across a situation where I had to call a method on generic object. Getting reflection in action worked for me.

public class SceneNode<T>{
    T operation;    
    public SceneNode() {
    }   
    public SceneNode(T operation) {
        this.operation = operation;
    }
    public void setOperation(T operation) {
        this.operation = operation;
    }
    public void doOperation() {
        Method m = operation.getClass().getMethod("execute");
        m.invoke(operation);
    }
}
0
1

Java is statically typed language. You must know the type at compile-time in order to be able to invoke a method. Instead of a subclass you can have an interface Executable that defines the execute() method. T (without any <T extends SomeClass>) has only the methods defined by java.lang.Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.