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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:26:27+00:00 2026-06-08T19:26:27+00:00

Lets look at the following example: public class BothPaintAndPrintable implements Paintable,Printable{ public void print()

  • 0

Lets look at the following example:

public class BothPaintAndPrintable implements Paintable,Printable{
    public void print() {}
    public void paint() {}
}
public interface Paintable {
    public void paint();
}

public interface Printable {
    public void print();
}

public class ITest {
    ArrayList<Printable> printables = new ArrayList<Printable>();
    ArrayList<Paintable> paintables = new ArrayList<Paintable>();
    public void add(Paintable p) {
        paintables.add(p);
    }
    public void add(Printable p) {
        printables.add(p);
    }
    public static void main(String[] args) {
        BothPaintAndPrintable a= new BothPaintAndPrintable();
        ITest t=new ITest();
        t.add(a);//compiliation error here
    }
}

What if I want BothPaintAndPrintable instances to be added to each of the ArrayLists?
One way would be overloading the method with a BothPaintAndPrintable parameter, but I’m trying to see alternatives since doing that might reduce code reuseability. Does anyone have another idea?

  • 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-08T19:26:29+00:00Added an answer on June 8, 2026 at 7:26 pm

    You need a third overload:

    public <T extends Object&Paintable&Printable> void add(T t) {
      paintables.add(t);
      printables.add(t);
    }
    

    This makes the erasure add(Object), so it doesn’t conflict with the other methods, but it does restrict the input to implementors of both Paintable and Printable.

    (Guava had to use this trick for Joiner with Iterator and Iterable, because some evil classes out there implemented both, even though it’s a terrible idea.)

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

Sidebar

Related Questions

Lets say i have the following set of interfaces.... public interface IStart { void
Let's look at the following day-to-day example of Java. package loop; final public class
Following is the simplest example of static inner class in Java. Let's look at
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
Let's suppose the following use case: for a working example plese look at this
So I have a class; lets use the ScrollViewer class as an example. It
Lets take a look at the following code: mongodb.js var mongo = require('mongodb') ,
Let's look at the following code snippet in Java. package division; import java.math.BigDecimal; final
I'm a bit confused regarding the document architecture. Lets look at MSN for Mac
I have multiple tables in a MySQL database. Lets say they look like 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.