Question: I'm writing an interactive fiction game in java from scratch. I'm currently storing all of my game object references in a hashmap in a singleton called ObjectManager. ObjectManager has a function called get which takes an integer ID and returns the appropriate reference. The problem is that it returns a BaseObject when I need to return subclasses of BaseObject with more functionality. 

So, what I've done so far is I've added a getEntity function which returns BaseEntity (which is a subclass of BaseObject). However, when I need the function to return to an object that is a subclass of BaseEntity that has added, required functionality, I will need to make another function. I know there is a better way, but I don't know what it is. I know very little of design patterns, and I'm not sure which one to use here. I tried passing 'class' as a parameter, but that didn't get me anywhere.

```
public BaseObject get(int ID){
    return (BaseObject)refMap.get(ID);
}

public BaseEntity getEntity(int ID){
    return (BaseEntity)refMap.get(ID);
}

```

Thanks, java ninjas!

Answer: 
Use a generic return type if you have a Java 5 runtime:

```
public <Entity extends BaseEntity> Entity getEntity(int ID){
    return (Entity)refMap.get(ID);
}

```
I would discourage you from proceeding with this design.

Google is [eradicating singletons from their Java code base](http://code.google.com/p/google-singleton-detector/); maybe it's a good idea for you, too.

It sounds like you're trying to do something that's been done well by Spring's ApplicationContext. You're trying to write a configurable, flexible object factory. It's been done. I'd look into dependency injection engines like Spring and Guice. Even if you decide not to use them, perhaps they can offer some design inspiration.

The best you can do is return an Object and cast.