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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:17:36+00:00 2026-06-03T03:17:36+00:00

With generic methods it’s possible to extend more than one type, e.g.: <T extends

  • 0

With generic methods it’s possible to extend more than one type, e.g.:

<T extends MyClass & MyInterface> void foo(T bar)

Is there a way to specify a List with a parameter which extends more than one type?

List<MyClass & MyInterface> myList;

doesn’t work…

This would allow the following:

class A extends MyClass implements MyInterface{}

class B extends MyClass implements MyInterface{}

myList.add(new A());
myList.add(new B());

MyClass c = myList.get(index);
MyInterface i = myList.get(index);

foo(myList.get(index));
  • 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-03T03:17:37+00:00Added an answer on June 3, 2026 at 3:17 am

    The answer is NO.

    Depending on your semantic expectation you will find a workaround

    One possible workaround for foo

       <T extends MyClass> void foo(T bar) {
         if (bar instanceof MyInterface) return;
       }
    

    Probably the best approach is to create a type supplying both type.
    The drawback is, all interesting classes needs to derived from that class

    abstract class MyClassInterface extends MyClass implements MyInterface {}
    
    List<MyClassInterface> myList;
    
    
    <T extends MyClassInterface> void foo(T bar)
    

    The naive approach just to supply both methods will cause ambiguities, so it is not a possible (for “AND”, for “XOR” it would be valid)

    <T extends MyClass> void foo(T bar)
    
    <T extends MyInterface> void foo(T bar)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I'm creating a generic class and in one of the methods I need to
Is it possible to use generic methods with WCF or ADO.NET DS? Or is
As you all know creating a web service with generic methods is not possible.
Currently I have one dao, that uses generic methods and hibernate criteria to perform
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: Why aren't Java Collections remove methods generic? Does anyone know why MyDamnedUnclearList.remove()
I lately wanted to learn more about generic methods and created several examples, but
I'm having trouble grasping generic methods. I have two classes that are generated (they
I'm not really familiar with creating generic methods, so I thought I'd put this

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.