• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using generic type

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is as follows:




My question is how can I use generic type to call the getter of the object?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The error here is that the generic class T might not have a method: getID, so the compiler throws an error here.

A solution can be to introduce an interface like:
'
and make this change:


Your Employee class must also implement the HasID interface.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But a simpler scheme would be to drop the generic in your TestGenericType class, and make the method 'showObject' generic instead. So:

So, if you have a List<Employee> emps, you can simply call:

With the previous setup you had to do:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . make the method 'showObject' generic instead. . . .

Piet, I think I would prefer that approach myself. Please explain a bit more, particularly about why you made the method static, in case OP isn't familiar with generic methods.

OP: this appears to be the first time you used the code button, but you didn't quite get it right. Please look here for a reminder of how code tags work. Because this was your first attempt, I have corrected the code tags, and doesn't the code look better Because you had three separate classes, I added three pairs of code tags and removed some empty space between them.
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here comes.

We have the class "TestGenericType<T>", with the instance method "public void showObject ( List<T> t) ...".
I suggested to change that to : TestGenericType<T extends HasID> (first reply).

But this way has two disadvantages:

First, before we can use the 'showObject' method, we need to create an instance of TestGenericType.
And second: suppose we have a class 'Employee implements HasID' and a class 'Teacher implements HasID' and that we want to use the 'showObject' method for both List<Employee> emps and List<Teacher> teachers. Give it a try.

So, that is not very handy. We can try to get rid of having to create an instance in order to use 'showObject'.
Let's try it:

And?

But why do we need to make TestGenericType generic in the first place? All that is needed is that the type of the parameter of 'showObject' has a method 'getID'. That'why I suggested to drop the generic part of the class and shift it to the method. We can then have:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic