0

I'm trying to make an abstract Object Factory and I tryied a few things and none of them worked.

What I would like to have is something like this that could be overrided:

public abstract ObjectFactory<Obj extends GameObject> {

    public abstract <Type extends Enum> Obj createObject (Type t) ;

}

that I could extends ObjectFactory and have:

public AntFactory extends ObjectFactory<Ant> {

     @Override
     public Ant createObject(AntType t) { }
}

but everything I tryied did not override the createObject method. The only thing that worked for me was:

public abstract ObjectFactory<Obj extends GameObject, Type extends Enum> {

    public abstract Obj createObject (Type t) ;

}    

Theres something that I'm not understanding about this concept or it doesn't work for enum.

Is there a way to keep the extends of enum only in the method and still override it?

MVAmorim
  • 105
  • 2
  • 3
  • 11
  • 2
  • 1
    You could have your enums all implement a common interface and parametrize your generic method with that. – Mena Sep 22 '16 at 15:08
  • @AndyTurner Still saying that I need to override the abstract method: public abstract > O createObject ( T type) ; and the method on the extended object: public Ant createObject(AntType type) – MVAmorim Sep 22 '16 at 15:11
  • 1
    @MVAmorim didn't say that would fix it; that's just how you correctly constraint the generic parameter. – Andy Turner Sep 22 '16 at 15:12
  • @Andy Turner I see, but the last one works that only extends enum (as all enum extends enum) – MVAmorim Sep 22 '16 at 15:13
  • @MVAmorim you actually don't need the generic parameter in the abstract class. `public abstract Obj createObject(Enum> t)` is equivalent. Then you override it with `public Ant createObject(Enum> t)`. Does this give a hint as to why your current approach doesn't work? – Andy Turner Sep 22 '16 at 15:15
  • @AndyTurner I know that it would work, but I need to keep the Enum>, and any kind of enum could be passed to the method, I dont want that. As I can see, theres no way to do this simple in java. Or I keep the last one i did – MVAmorim Sep 22 '16 at 15:19
  • 1
    @MVAmorim then you have to have the type variable on the class, not the method, as in your last example. – Andy Turner Sep 22 '16 at 15:19
  • @Andy Turner Yeah, that what I did last. I see no problem in that, just wanted to know if theres a simple way of keeping it in the method, but there is not. Thanks for your replies. – MVAmorim Sep 22 '16 at 15:21

0 Answers0