Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:43:31+00:00 2026-06-05T20:43:31+00:00

I’m new to Java and have gotten myself into a situation where it’s evident

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:43:33+00:00Added an answer on June 5, 2026 at 8:43 pm

    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<?>> {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.