Results 1 to 2 of 2
  1. #1
    mleather is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default Using Generics to execute implemented functions


    I'm trying to use generics to take an object and then execute the object using returned function.

    I've simplified it below, but the gist is there. The "stuff.doStuff(obj)" call causes a compile time error that basically says that obj needs to be of type <? extends Object> instead of Object.

    Am I fundamentally wrong here? Is there a way to cast it?

    Thanks for taking a look.

    public class Main {

    public void notMain(Object obj) {
    Stuffer<? extends Object> stuff = getStuffer(obj.getClass());


    private <T> Stuffer<T> getStuffer(Class<T> clazz) {
    // retrieve instance based on class

    private interface Stuffer<T> {

    public void doStuff(T stuffToDo);

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    I'm not sure where you are trying to go, but your use of generics won't get you anywhere. If you want a variety of classes to do one thing, create an interface and have them all implement it. The interface will provide the common method that can be called "generically". Handle all the various instances using a field of the type of the interface.

Similar Threads

  1. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  2. Class Reflection: Finding implemented interfaces
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:09 PM
  3. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  4. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM
  5. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts