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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:09:01+00:00 2026-06-14T12:09:01+00:00

I would like to declare attribute that holds instance of class that implements 2

  • 0

I would like to declare attribute that holds instance of class that implements 2 different interfaces. I have tried this syntax:

private <? extends Interface1 & Interface2> name;

and this:

private <T extends Interface1 & Interface2> T name;

None of those work. Is it possible? What’s the syntax? I’m trying to avoid declaring another interface that inherits from both Interface1 and Interface2.


Edit:

The class containing this attribute should not have any type arguments. That is nothing like this:

public class MyClass<T extends Interface1 & Interface2>{
    private T name;
    ...
}

It would not make any sense for those using the class. It is not expected neither logical not possible for that class to be generic.

  • 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-14T12:09:02+00:00Added an answer on June 14, 2026 at 12:09 pm

    A variable cannot be generic.

    private <T> T var;
    

    is not possible – at which point is T defined? When accessing var, I cannot make much assumptions on what I used at assignment time.

    Java allows generics on classes and on methods. So you can have

    private <T implements Cloneable & Serializable> void setVar(T val);
    

    and you can have a class-wide type T.

    But always remember that in the end, it is implemented by type erasure. You can always emulate more complex logic using getters, setters and casts. When done properly, it will give you just as much type safety.

    The simplest way to obtain a variable with the type safety you want is to just use two variables, and a setter to keep them in sync.

    private Serializable vars;
    private Cloneable vars;
    

    will of course give you a good type safety. But yes, it needs 4 bytes of additional memory, and a setter.

    Here’s the casting approach you asked:

    private Object internal_var;
    
    // Implementation notice: do not remove this generic.
    // Due to a Java limitation, we *do* want these two constraints!
    public <T extends Serializable & Cloneable> void setVar(T val) {
        internal_var = val;
    }
    
    public Serializable getSerializable() {
        return (Serializable) internal_var; // Type checked in setter!
    }
    
    public Cloneable getCloneable() {
        return (Cloneable) internal_var; // Type checked in setter!
    }
    
    // This is the way to use it in a generic getter:
    public <T extends Serializable & Cloneable> T getVar(Class<? super T> cls) {
        return (T) cls.cast(val);
    }
    

    Note that in order to use T in the getter, we do need to have a parameter involving T.
    Assuming we know a class Example implements Serializable, Cloneable, we can then use

    // This actually ensures we get an instance of `Example` out:
    Example e = instance.getVar(Example.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to simply declare a read only attribute in Moose that cannot
I have defined an assembly level attribute class FooAttribute like this: namespace Bar {
Pretty simple question - I have an attribute that I would like to have
i would like to do this: DECLARE @TmpTable TABLE = select * from someTable
I would like to know how do I declare a record, that has some
I would declare an empty String variable like this: string myString = string.Empty; Is
I have written my own function, which in C would be declared like this,
I've implemented a class that looks like this interface: [ImmutableObject(true)] public interface ICustomEvent {
I have to work with multiple SQL Server tables that generally look like this:
I have an XML data file that looks like this: <Sales> <Store Number=8 Date=2012-08-20T03:00:00>

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.