0

How resolve this

how change code to get same result

public class myClass
{

   List<Drawable> d;
   List<Bitmap> b;
    public myClass(Integer[] resIDsList)
    {
        ...
    }

    public myClass(List<? extends Drawable> drawableList) // error occure here
    {
        d = drawableList;
    }

    public myClass(List<? extends Bitmap> bitmapList) // and here too
    {
        b = bitmapList;
    }

}

if constructors are same in above?

4
  • 1
    First of all, I'd say that you should actually have two classes there: the way you construct your class implies it has two different responsibilities, which is not right in terms of design. Commented Apr 16, 2017 at 20:35
  • However, on a rare occasion where some constructor logic can accept different lists and still not violate single responsibility principle, one can use factory methods with different names to construct objects. Commented Apr 16, 2017 at 20:38
  • @M.Prokhorov three classes: there are 3 constructors there. Commented Apr 16, 2017 at 20:45
  • @AndyTurner, depending on what those integers are. Those may be some identifiers by which one can find a set of bitmaps. Although it's usually better to have a find-by-id somewhere outside to reduce coupling of both parts. Commented Apr 17, 2017 at 11:16

3 Answers 3

5

Define static factory methods, and make the constructor private:

static myClass fromDrawables(List<? extends Drawable> list) {
  return new myClass(list, null);
}

static myClass fromBitmaps(List<? extends Bitmap> list) {
  return new myClass(null, list);
}

private myClass(List<? extends Drawable> drawables, List<? extends Bitmap> bitmaps) {
  // ...
}

(You probably want to add another factory method for the myClass(Integer[]) case; but I hope you get the idea of how to extend the above code for this).

Now you would invoke the factory methods, rather than the constructor:

// Not new myClass(...)
myClass a = myClass.fromDrawables(drawablesList);
myClass b = myClass.fromBitmaps(bitmapsList);

I'd recommend reading Effective Java 2nd Ed Item 1: "Consider static factory methods instead of constructors" for a thorough discussion of this approach.

Sign up to request clarification or add additional context in comments.

Comments

1

Due to type erasure, this will not compile. One solution would be to pass the elements to the constructor and simply add them to each respective List.

public myClass(Drawable... drawables) {
    d = new ArrayList<>(Arrays.asList(drawables));
}

public myClass(Bitmap... bitmaps) {
    b = new ArrayList<>(Arrays.asList(bitmaps));
}

4 Comments

Wouldn't work: there's no addAll that accepts an array.
@M.Prokhorov My bad, it can be viewed as a List instead.
d = new ArrayList<>(Arrays.asList(drawables)) would be more concise and efficient (it avoids resizing the backing array).
@AndyTurner Very true, I totally forgot about that constructor.
-1

You will need to change your List<Drawable> d; to List<? extends Drawable> d; outside of your method. Do this for your List<Bitmap> b; as well. Afterwards, your class should look like this:

public class myClass
{

  List<? extends Drawable> d;
  List<? extends Bitmap> b;
   public myClass(Integer[] resIDsList)
   {
       ...
   }

   public myClass(List<? extends Drawable> drawableList) // error occurs here
   {
       d = drawableList;
   }

   public myClass(List<? extends Bitmap> bitmapList) // and here too
   {
       b = bitmapList;
   }

}

You could also merge both of those constructors into one if you wanted, with this:

public myClass(List<? extends Drawable> drawableList, List<? extends Bitmap> bitmapList)
{
   d = drawableList;
   b = bitmapList;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.