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 644363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:22:57+00:00 2026-05-13T21:22:57+00:00

I have an interface parametrized on a type which extends a parametrized type, and

  • 0

I have an interface parametrized on a type which extends a parametrized type, and that interface contains a method which needs the type parameter of the type being extended. In code, this is what I want:

class Thingy<X> { ... }

interface Foo<P extends Thingy<?>> {
  <T> T frobnicate(P<T> a);
}

But that doesn’t compile. In order to properly frobnicate a Thingy, it’s necessary to know the type of Thingy it is, but also some Foos may be specialized for handling only particular kinds of Thingys (the exception being that they are required to be able to handle whatever type T any Thingy is parametrized on).

(BTW, I’ve already spotted these questions. The former is about requiring only one generic parameter; I don’t care if I need one, two, or five here, so long as there’s some solution. The latter has a specific type rather than a wildcard in the class corresponding to my Foo, so the solution there isn’t applicable here, so far as I can see.)

EDIT:

Since the number of subclasses of Thingy I intend to have is small, I can get the same effect by subclassing Thingy and creating another interface just for handling those:

interface Foo {
  <T> T frobnicate(Thingy<T> a);
}

class SpecialThingy<T> extends Thingy<T> { ... }

interface Bar {
   <T> T frobnicate(SpecialThingy<T> a);
}

That’s not as elegant as I would have liked, but it will do the job in this case.

  • 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-05-13T21:22:57+00:00Added an answer on May 13, 2026 at 9:22 pm

    Instead of using a type parameter which extends Thingy<T>, use a type parameter for just the T and add the Thingy part to where it is needed:

    class Thingy<X> { }
    
    interface Foo<X> {
      X frobnicate(Thingy<X> a);
    }
    

    Or if you need to use a specific subclass of Thingy, then how about this?

    class Thingy<X> { }
    
    interface Foo<X, T extends Thingy<X>> {
      X frobnicate(T a);
    }
    

    If you require something more complex, then the simplest thing would be to just have runtime type checks. Sometimes generics produce more unnecessary complexity than they are worth.

    It’s also possible that with a better design you could avoid the need for expressing such things with generics. To give suggestions about that, it would be necessary to know more about how the Thingies are used and have a more concrete example than Thingies and Foos.

    EDIT:

    Since the requirement is that Thingy’s type paramerer can not be known beforehand, then this should be the only option:

    interface Foo {
      <T> T frobnicate(Thingy<T> a);
    }
    

    Follow the Liskov substitution principle and make it work for all implementations of Thingy. If the code can work only for some subtypes of Thingy, then I don’t think that you can express it with generics (it would require giving type parameter for a type parameter), and the only option is to have runtime type checks. And runtime type checks are a code smell, so it’s best to fix the design so that it’s not needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just thought I'd update in-case anyone stumbles across this page… May 15, 2026 at 2:30 pm
  • Editorial Team
    Editorial Team added an answer I think there are 2 options. Using expando. You can… May 15, 2026 at 2:30 pm
  • Editorial Team
    Editorial Team added an answer Is the PHP script encoded in UTF-8? If it is,… May 15, 2026 at 2:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.