Hi,
say I have this method:
void <T> foo (T arg) {
arg.bar();
}
The compiler complains, that there is no method bar() declared on type
T. Well, it's a /generic/ type, so of course you can't tell without
instantiating the template first with a real type, but it won't even let me!
Where is the benefit of using generics, if I can't even call methods on
them, except those declared on Object, which every type has?
I have to say, I am confused.

Signature
Matthias Kaeppler
Ingo R. Homann - 22 Jun 2005 11:57 GMT
Hi,
> Hi,
>
[quoted text clipped - 8 lines]
> instantiating the template first with a real type, but it won't even let
> me!
That's not a specific generic-problem. The following (without generics)
wouldn't work, either:
void Object foo (Object arg) {
arg.bar();
}
Try the following instead:
void <T extends Barable> foo (T arg) {
arg.bar();
}
Ciao,
Ingo
Bjorn Abelli - 22 Jun 2005 12:04 GMT
"Matthias Kaeppler" wrote...
> say I have this method:
>
[quoted text clipped - 6 lines]
> tell without instantiating the template first with a real type,
> but it won't even let me!
Of course it will, if you somehow tell it that T is of a type that has the
method bar implemented. Otherwise your example would be just the same as:
void foo (Object arg) {
arg.bar();
}
...and to my knowledge, Object doesn't have bar implemented.
> I have to say, I am confused.
So you need to read up on the syntax for using generics.
Here's a small example that might clarify things for you:
class TY<T extends Y>
{
public void foo (T arg)
{
arg.bar();
}
}
interface Y
{
public void bar();
}
class X implements Y
{
public void bar()
{
System.out.println("bar");
}
}
public class TGen
{
public static void main (String[] args)
{
X x = new X();
TY<Y> tt = new TY<Y>();
tt.foo(x);
}
}
// Bjorn A
Matthias Kaeppler - 22 Jun 2005 12:33 GMT
> Of course it will, if you somehow tell it that T is of a type that has the
> method bar implemented. Otherwise your example would be just the same as:
>
> void foo (Object arg) {
> arg.bar();
> }
Why Object? It should be replaced by the type I provide, not Object.
Otherwise I can use as well no generics at all, because where is the
benefit?
The solution to move all the methods in the body of the template to a
separate interface is just inconvenient in pratice.

Signature
Matthias Kaeppler
Owen Jacobson - 22 Jun 2005 13:11 GMT
>> Of course it will, if you somehow tell it that T is of a type that has
>> the method bar implemented. Otherwise your example would be just the
[quoted text clipped - 7 lines]
> Otherwise I can use as well no generics at all, because where is the
> benefit?
For this specific pattern, there is no benefit to using generics. This is
not the problem they're designed to solve.
> The solution to move all the methods in the body of the template to a
> separate interface is just inconvenient in pratice.
Your mistake is in thinking of Java generics as a templating mechanism.
While both Java generics and templates provide generic programming, they
approach it from different directions.
Stop Doing That. From your original example, there's no reason to use
generics at all:
interface T {
public void bar ();
}
...
public void foo (T arg) {
arg.bar ();
}
...
provides exactly the semantics any combination of generics I can think of
would provide for your code.
Matthias Kaeppler - 22 Jun 2005 16:55 GMT
> interface T {
> public void bar ();
[quoted text clipped - 8 lines]
> provides exactly the semantics any combination of generics I can think of
> would provide for your code.
That's correct, but this would mean another change
in design, which I don't want.
I think my problem was to confuse Java Generics
with C++ templates, which can exactly do what I
need, without changing any code structure and
design and I mistakenly thought Java had adopted
the template design from C++.
I'll find a workaround somehow.
Regards,
Matthias
Alan Krueger - 22 Jun 2005 17:17 GMT
>> interface T {
>> public void bar ();
[quoted text clipped - 11 lines]
> That's correct, but this would mean another change in design, which I
> don't want.
It's not really a change in design, it's making explicit an aspect of
the design that's implicit in the C++ code. You're calling a method of
what is essentially an anonymous interface. Java simply requires you to
make that explicit.
It's similar translating code that uses C++ function pointers; you can't
do it directly, you need to associate it with an interface or class.
Bjorn Abelli - 22 Jun 2005 13:40 GMT
"Matthias Kaeppler" wrote...
>> Of course it will, if you somehow tell it that T is of a
>> type that has the method bar implemented. Otherwise your
[quoted text clipped - 6 lines]
> Why Object? It should be replaced by the type I provide,
> not Object.
Because you haven't provided any other information on "T", such as
restrictions on what types it's supposed to be "replaced" with, so the type
you "provide" could as well be just Object, as any other class.
> Otherwise I can use as well no generics at all, because where
> is the benefit?
I agree that the use of generics seem pointless in the example *you*
provided. The benefits are elsewhere.
http://www.langer.camelot.de/GenericsFAQ/FAQSections/Fundamentals.html
> The solution to move all the methods in the body of the template
> to a separate interface is just inconvenient in pratice.
Java generics are not to be confused with "templates". IMHO, the generics
main merits lies in the possibilities of more type-safety and the lesser
need of explicit castings.
// Bjorn A
Chris Uppal - 22 Jun 2005 13:03 GMT
> The compiler complains, that there is no method bar() declared on type
> T. Well, it's a /generic/ type, so of course you can't tell without
> instantiating the template first with a real type, but it won't even let
> me!
You are thinking that Java's generics are similar to C++'s templates. Which is
understandable but, unfortunately, completely wrong. There is no significant
similarity -- they do rather different things, and do them in /completely/
different ways. In particular, Java's generics are /not/ a templating
facility, the compiler does not "instantiate" a template by "substituting in"
the parameter class(es).
I suggest that you:
a) forget about templates when you are working in
Java -- they'll just confuse you.
b) read (several times, unless you are quicker on the
uptake than me) Gilad Bracha's generics tutorial.
I think the tutorial is here:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
-- chris
Matthias Kaeppler - 22 Jun 2005 16:51 GMT
> You are thinking that Java's generics are similar to C++'s templates.
I think I'm guilty in this part :)
> I think the tutorial is here:
> http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Thanks, I'll give it a shot asap.