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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:23:16+00:00 2026-06-14T00:23:16+00:00

How do you make this work: public class Frankenstein<T extends IHuman, IMonster>{ } Without

  • 0

How do you make this work:

public class Frankenstein<T extends IHuman, IMonster>{
}

Without making

public interface Weirdo extends Ihuman, IMonster{
}

Edit

Why is this not working?

public <T> void mapThis(
        Class<? extends MyClass<T>> key, Class<? extends T & IDisposable> value) {

}

I am getting compiler message marking Class<? extends T & IDisposable> as an Error.

  • 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-14T00:23:18+00:00Added an answer on June 14, 2026 at 12:23 am

    Reimeus already pointed out that what you’re asking for in your edit isn’t possible. I’d just like to expand a little on why.

    One would think you could use the following:

    public <T, U extends T & IDisposable> void mapThis(
            Class<? extends MyClass<T>> key,
            Class<? extends U> value
    ) { ... }
    

    In fact that’s what came to my mind when I first saw this post. But this actually gives a compiler error:

    a type variable may not be followed by other bounds

    To help me explain why, I’d like to quote an Oracle Blogs post by Victor Rudometov about this error:

    This fact is not always clear, but it is true. The following code
    should not compile:

    interface I {}

    class TestBounds <U, T extends U & I> {

    }

    Because JLS Chapter 4
    Types, Values, and Variables section 4.4 Type Variables states: "The
    bound consists of either a type variable, or a class or interface type
    T possibly followed by further interface types I1 , …, In.
    ". So one
    may use T extends U, T extends SomeClass & I, but not T extends U & I.
    This rule applies to all cases including type variables and bounds in
    methods and constructors.

    The reasons for this restriction are explored in a closely related post: Why can't I use a type argument in a type parameter with multiple bounds?

    To summarize, the restriction was imposed in order to "preclude certain awkward situations coming into existence" (JLS §4.9).

    What kind of awkward situations? An answer by Chris Povirk describes one:

    [A reason for the restriction is] the possibility of specifying illegal types. Specifically, extending a generic interface twice with different parameters. I can’t come up with a non-contrived example, but:

    /** Contains a Comparator<String> that also implements the given type T. */
    class StringComparatorHolder<T, C extends T & Comparator<String>> {
      private final C comparator;
      // ...
    }
     
    void foo(StringComparatorHolder<Comparator<Integer>, ?> holder) { ... }
    

    Now holder.comparator is a Comparator<Integer> and a Comparator<String>.

    Chris also points to Sun bug 4899305, which was a bug contesting this language restriction. It was closed as Won’t Fix with the following comment:

    If a type variable could be followed by type variables or by (possibly
    parameterized) interfaces, there would likely be more mutually
    recursive type variables, which are very difficult to handle. Things
    are already complicated when a bound is simply a parameterized type,
    e.g. <S,R extends Comparable<S>>. Consequently, bounds are not going
    to change now. Both javac and Eclipse agree that S&T and
    S&Comparable<S> are illegal.

    So those are the reasons behind the restriction. Addressing generic methods specifically (which your question concerns), I’d like to further point out that type inference would theoretically cause such bounds to be pointless anyway.

    If we reexamine the type parameters declared in the hypothetical signature above:

    <T, U extends T & IDisposable>
    

    Assuming the caller isn’t explicitly specifying T and U, this can be reduced to the following:

    <T, U extends Object & IDisposable>
    

    Or just this (subtle difference, but that’s another topic):

    <T, U extends IDisposable>
    

    This is because T doesn’t have any bounds, so no matter what type of arguments get passed in, T can always resolve to Object at the very least, and so then can U.

    Let’s go back and say T is bounded:

    <T extends Foo, U extends T & IDisposable>
    

    This can be reduced in the same way (Foo could be a class or interface):

    <T extends Foo, U extends Foo & IDisposable>
    

    Based on that reasoning, the syntax you’re trying to achieve is pointless as far as restricting the caller to more specific arguments.

    Pre-Java 8 addendum:

    Prior to Java 8, there is a use case for what you’re trying to do. Because of a limitation with how the compiler infers generic method type parameters, my above reasoning to go out the window. Take the following generic method:

    class MyClass {
        static <T> void foo(T t1, T t2) { }
    }
    

    This is a common beginner’s mistake of trying to make a method that takes two parameters of the "same type". Of course it’s pointless because of the way inheritance works:

    MyClass.foo("asdf", 42); // legal
    

    Here, T is inferred to be Object – this matches up with earlier reasoning about simplifying the mapThis type parameters. You have to manually specify the type parameters in order to achieve the intended type checking:

    MyClass.<String>foo("asdf", 42); // compiler error
    

    However, and here’s where your use case starts to come in, it’s a different matter with multiple type parameters with staggered bounds:

    class MyClass {
        static <T, U extends T> void foo(T t, U u) { }
    }
    

    Now this call errors:

    MyClass.foo("asdf", 42); // compiler error
    

    The tables have turned – we have to manually relax the type parameters to get it to compile:

    MyClass.<Object, Object>foo("asdf", 42); // legal
    

    This happens because of the limited way in which the compiler infers method type parameters. For this reason, what you wanted to achieve would’ve actually had an application in restricting the caller’s arguments.

    However, this problem appears to have been fixed in Java 8, and MyClass.foo("asdf", 42) now compiles without any error (thanks to Regent for pointing this out).

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

Sidebar

Related Questions

I am struggling to make this work: public abstract class MapperFactory<M extends TaskMapper<? extends
How could I make this work?: public class myClass { public string first; public
Is there any way to make a code like this work: public class Func2<A,
Is there any way to make this work in Java? public static void change(List<?
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
How can i make an extension method that will work like this public static
Not sure exactly what I need to do to make this work, so my
I have this type of class hierarchy: public interface IA{} public interface IB{void Foo();}
how can I make this work? $current_id=5; SELECT Id FROM table1 WHERE Output='yes' and
I have this code and I need to make this work: if ($handle =

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.