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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:23:26+00:00 2026-06-11T05:23:26+00:00

I have two interfaces. An interface A, and interface B, which extends interface A.

  • 0

I have two interfaces. An interface A, and interface B, which extends interface A. Now, I have class which likes to keep reference of object that either implements A or B. And I would like to provide setter for it. I tried below, but getting type mis match exception.

public interface A {
}

public interface B extends A {
}

public class AB {
private Class<? extends A> object;

public void setObject(Class<? extends A> o){
   this.object = o;
}
}

So basically, I would like setObject method to accept an object that either implements interface A or B.

  • 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-11T05:23:27+00:00Added an answer on June 11, 2026 at 5:23 am

    Simple answer:

    Type it as A: setObject(A a).

    A class that implements B also implements A. Full code:

    public class UsesA {
      private A a;
    
      public void setObject(A a){
        this.a = a;
      }
    }
    

    Now, if you really want to work with B, you’d type it as B, which would also allow you to treat it as an A, since B inherits from it:

    public class UsesB {
      private B b; // can call A's methods on this field
    
      public void setObject(B b) {
        this.b = b;
      }
    }
    

    But now you can’t pass an A (static) reference to setObject. If you really want to do this, then you’d need to first downcast it as a B, which could fail at runtime. Generics will not improve on this.

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

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have a class A which implements the interface serializible. There are two sub
I have the following two interfaces: public interface IMembershipProvider { object Login(ILoginProviderParameters loginParameters); void
I have two interfaces IHeaderRow , and IDetailRow I then have an object that
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and
I have an interface (say Employee) which has 2 classes that implements it (say
I have my OrganizationRequestContext interface, which works great: @Service(OrganizationDAO.class) public interface OrganizationRequestContext extends RequestContext
We have an idl file with multiple interfaces defined, two of which have someting
Lets say I have two interfaces interface A and interface B: public interface A
I have two basic interface-related concepts that I need to have a better understanding

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.