0
public class method
{
    private static class Foo
    {
        public void hello() { System.out.println("Foo"); }
    }

    private static class Bar
    {
        public void hello() { System.out.println("Bar"); }
    }

    private static <T> void hello(T typ)
    {
        typ.hello();
    }

    public static void main(String args[])
    {
        Foo foo = new Foo();
        Bar bar = new Bar();
        hello(foo);
        hello(bar);
    }
}

I've looked at the other questions here regarding generics, but despite everything I've seen there and applied to the code I've written, I'm still having problems with my Java code. I've boiled the problem I'm having to the code above. When I try to compile the codd above, I get the following error:

method.java:15: error: cannot find symbol
        typ.hello();
           ^
  symbol:   method hello()
  location: variable typ of type T
  where T is a type-variable:
    T extends Object declared in method <T>hello(T)

It could be that I'm tr6ing to do something with generic that they were not designed to do, but based on my understanding of the docum3ntation, this should work. Of course I read the documentation with the idea that I could do something like this, which certainly may have influenced my understanding of it.

Thanks

2
  • I'm really trying to duplicate C++ templated member functions template<class T> hello(T typ) { } Commented May 17, 2013 at 21:09
  • If I understand your problem, you have two interfaces Foo and Bar. Both interfaces have a method named hello(). You want to create a method like private static <T> void hello(T typ), where T is either Foo or Bar. Did I get it right? Commented May 21, 2013 at 12:40

4 Answers 4

4

Here's your problem:

private static <T> void hello(T typ)

T doesn't extend anything that implements a method named "Hello".

Instead, replace it with

 private static <T extends ClassThatHasHello> void hello(T type)

and the class:

 public class ClassThatHasHello {
      public void hello() { }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

I see, but in this case I've got multiple classes
I'm really trying to duplicate C++ templated member functions template<class T> hello(T typ) { }
That's fine, just use an interface instead of a class for ClassThatHasHello. Have your multiple classes implement it
1

You defined T as subtype of Object. without any type restriction. Object has no hello() method

You could create an interface or super type for the Foo and Bar Then <T extends SuperType>

6 Comments

A supertype might work since I'm trying to write the generic for 2 classes that I didn't write, each of which have the same method
so you have 4 classes A,B,C,D all have hello(), you created a supertype (abstract class/Interface/normal class) S it has hello() too. let A-D extend/implement the S. then you can write <T extends S> and compiler won't complain about typ.hello()
Unfortunately I didn't write Foo or Bar (which are actually interfaces, not as I've shown them here -- but the error is the same
then show us your real classes. I think I explained the idea in answer+comment clear enough. pls show your related codes if it still doesn't work there.
downvote is ok, but pls leave a comment, so that I can learn why it is bad. thx.
|
0

You need to use an interface. This works:

private interface Hello {
  public void hello();
}

private static class Foo implements Hello {
  @Override
  public void hello() {
    System.out.println("Foo");
  }
}

private static class Bar implements Hello  {
  @Override
  public void hello() {
    System.out.println("Bar");
  }
}

private static <T extends Hello> void hello(T typ) {
  typ.hello();
}

public static void main(String args[]) {
  Foo foo = new Foo();
  Bar bar = new Bar();
  hello(foo);
  hello(bar);
}

6 Comments

Unfortunately, I'm trying to write the generic for 2 classes I did not write (actually interfaces, each of which extend other interfaces, with some overloading)
In that case you are asking how to dynamically make an object implement an interface - you may be looking for the Proxy class.
I think it's just easier to copy/paste the 4 lines for each of the 2 classes in an if statement. I thought I could save the double maintenence of copy/paste, but java seems to be deficient in that regard as compared to C++ where this would have worked as a templated method
You may live to regret that decision - that is neither a threat nor a promise. :)
I most likely will, but adding a whole Proxy class implementation to get around 4 lines of code seems like it's more likely to cause problems given the added complexity. The architecture on this project leaves a lot to be desired: Multiple interfaces all implenting the same methods without extending a single, common interface, and where the common interface is implemented, several of the interfaces overload the common methods, so extending the common interface won't work because it's overloaded
|
-2

you are using generics, and you are trying to call the hello method of type T, but you dont know what type is that. if you are sure it has hello method, use casting

7 Comments

I don't think casting will work here anyway since I'm passing in 2 different classes and I don't know which it is
if both have hello method,you can create an interface, make them implement it and then cast to the interface
They already are interfaces. I'm just trying avoid a lot of copy paste since both of them implement the same method that I've got to call, giving exactly the same parameters. Looks like the only solution is copy/paste I miss C++ :-(
no its not the only solution, i cant understand why you cant use casting?
as the interface they both implement
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.