Search code examples
javagenericsbounded-wildcard

Java Generics - wildcards


I'm new to Java and have gotten myself into a situation where it's evident that I'm misunderstanding something about how it handles Generics, but reading tutorials and searching stackoverflow hasn't (at least so far) given me clarity beyond that I suspect I'm misusing wildcards. As a heads up I have a C++ background, so how it deals with templates is probably coloring how I've approached this.

Here's the basic structure of my inheritance using representative classes

abstract class PacketHeader{
   // some stuff
}

class TypeOfPacketHeader extends PacketHeader{
   // extended stuff
}

abstract class Packet<T extends PacketHeader>{
    T mHeader;
    // some methods treating T as a type of PacketHeader
    // some abstract methods
}

class TypeOfPacket extends Packet<TypeOfPacketHeader>{
    static TypeOfPacket obtain {
        return new TypeOfPacket();
    }
    // overriden abstract functions that call specific TypeOfPacketHeader methods on mHeader
}

interface PacketParser<T extends Packet<? extends PacketHeader>>{
T obtainPacket();
        void parse(T packet);
}

class ImplementedPacketParser implements PacketParser<TypeOfPacket>{
     TypeOfPacket obtainPacket(){
         return TypeOfPacket.obtain();
     }
     void parse(TypeOfPacket packet){
         // code that relies on TypeOfPacket specific functions
     }
}

That seems to all be correct (or at least eclipse isn't complaining), issues seem to arise when I'm attempting to make use of them. My first attempt was:

class User{
    PacketParser mParser;

    User(PacketParser parser){
        mParser = parser;
    }

    void DoSomething(){
         Packet packet = mParser.obtainPacket();
         // do some stuff with the packet
         mParser.parse(packet);
    }
}

and led to warnings of Raw types. So I tried...

class User{
    PacketParser<? extends Packet<? extends PacketHeader>> mParser;

    User(PacketParser<? extends Packet<? extends PacketHeader>> parser){
        mParser = parser;
    }

    void DoSomething(){
         Packet<? extends PacketHeader> packet = parser.obtainPacket();
         // do some stuff with the packet
         mParser.parse(packet);
    }
}

But this leads to an error that

The method parse(capture#9-of ? extends Packet) in the type PacketParser> is not applicable for the arguments (Packet)

At this point I've decided that I'm misunderstanding something about how the generics are working, so I've turned to stackoverflow to hopefully point me to where I've gone wrong and maybe point me in the right direction.


Solution

  • In your User code, the compiler doesn't know that the ? extends Packet<? extends PacketHeader> on the mParser field is the same type as the Packet<? extends PacketHeader> packet local variable.

    You need to bind the type of packet that will be used by making User generic:

    class User<T extends Packet<?>> {
        PacketParser<T> mParser;
    
        User(PacketParser<T> parser){
            mParser = parser;
        }
    
        void DoSomething(){
             T packet = parser.obtainPacket();
             // do some stuff with the packet
             mParser.parse(packet);
        }
    }
    
    // and then when instantiating your User, specify the packet type:
    new User<TypeOfPacket>(new ImplementedPacketParser())
    

    Now the compiler knows that the Ts it sees represent the same type of Packet<?>, whereas each time it sees ? extends Packet<?> could be different subtypes of Packet<?>.

    Edit: An additional note: the "? extends PacketHeader" is not needed in the PacketParser interface declaration, because the Packet class already limits its T to PacketHeader and its subtypes.

    // ? is implicitly constrained to "extends PacketHeader" because of
    // the way Packet's generic is defined
    interface PacketParser<T extends Packet<?>> {