Java generics: how the return the same type defined as a wildcard on a method's input ?

Posted on 2007-10-09
Last Modified: 2010-03-30
Hi experts,

I would like to have the following work with generics.
Let's say I have a service facade class. This class has methods for use cases. Each method will open a session (database session), execute a series of DAOs under the same session and at the end, close it. For that purpose, I have a SessionManager class with one method: executeWorkUnit(). This method will contain logic for opening and closing the shared session, and delegate the actual logic code to the responsible unit. This unit will be an instance of ISessionWorkUnit. This interface defines one method called performWork(). This method is to be extended by implementing classes and will optionally return an Object.. the result of that work (could be a domain object retrieved from the database).

So my question is how to do this with generics. I want that the return type of the ISessionWorkUnit's executeWorkUnit be defined with generics. For this I have the code:

public interface ISessionWorkUnit<T> {
      public T performWork();

public class SessionManager {
      public Object executeWorkUnit(ISessionWorkUnit<?> workUnit) {
            return workUnit.performWork();

returnObject =
  (IMyObject) sessionManager.executeWorkUnit(new ISessionWorkUnit<IMyObject>(){
     public IMyObject performWork() {
        IMyObjectDAO myObjectDao= new IMyObjectDaoImpl();
        return myObjectDao.createMyObject();

As you can see in the code above, I still have to cast the object to the right type on the executeWorkUnit method. This is because I'm forced to define its return type as Object. How can I defined that it will return the exact same wildcard type that is defined on the method's input parameter ?
Question by:MindWalker
    LVL 13

    Accepted Solution

    Change the SessionManager to:

    public class SessionManager {
          public <T> T executeWorkUnit(ISessionWorkUnit<T> workUnit) {
                return workUnit.performWork();


    Author Comment

    That worked sweet as a charm! Damn... I really need to read some extensive info about generics. What does that notation mean exactly ?

    So you're accepting as an argument a parametrized object of type T, and you're returning an instance T of that parametrized object T ?
    LVL 13

    Expert Comment

    It means you can pass a typed ISessionWorkUnit and the compiler will check that the return type of the method is of the specified type.

    Don't know if whay I'm saying makes any sense though.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now