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