Shinigami
Shinigami

Reputation: 1055

JAVA - Return list of interface

I have this problem, I'd like to return a list of interface and implementation class only after the if block.

public interface Lotto { }

public class LottoImplSecond implements Lotto { }
public class LottoImplFirst implements Lotto { }

public class MyClass {
   public List<Lotto> getLotto(Integer number){
       if(number==1) List<Lotto> listaLotto=new ArrayList<LottoImplFirst>();
       else if(number==2) List<Lotto> listaLotto=new ArrayList<LottoImplSecond>();
   return listaLotto;
}

Upvotes: 3

Views: 8612

Answers (7)

dcernahoschi
dcernahoschi

Reputation: 15240

First List<Lotto> is not a supertype of List<LottoImplSecond> or List<LottoImplFirst> so you need to return a List<? extends Lotto>

The problem with the list List<? extends Lotto> is that you can only get from it and never add to it (safely, without a compiler warning)

Upvotes: 4

Marko Topolnik
Marko Topolnik

Reputation: 200148

Your lines

if(number==1) List<Lotto> listaLotto=new ArrayList<LottoImplFirst>();
else if(numeber==1) List<Lotto> listaLotto=new ArrayList<LottoImplSecond>();

are achieving nothing as far as the code that calls your method is concerned. Those two lists are absolutely indistinguishable for your caller. I suggest you to just write

listaLotto = new ArrayList<Lotto>();

Adapt the return type accordingly and be done with it.

A side note: your code is not compilable Java, but it is a minor point.

Upvotes: 2

Amit Deshpande
Amit Deshpande

Reputation: 19185

Condition is same in both if and else. Generics inheritance is different than Java inheritance.

 public List<Lotto> getLotto(Integer number)
{
    return ((List<Lotto>)(number == 1 ? new ArrayList<LottoImplFirst>() : new ArrayList<LottoImplSecond>());
}

Upvotes: 0

Edd
Edd

Reputation: 3822

There are a number of mistakes in your code, but I think what you want to do is change the getLotto method signature to:

public List<? extends Lotto> getLotto(Integer number);

You'll have to define listaLotto outside of the if statement too, so that it is still in scope when you try and return it.

Also note that there's a typo on the else if condition - I presume it should test if number == 1, although at present this will never execute as the original if condition tests if number == 1 also.

The complete solution (assuming the second value is 2) is something like the following:

public interface Lotto { }

public class LottoImplSecond implements Lotto { }
public class LottoImplFirst implements Lotto { }

public class MyClass {
   public List<? extends Lotto> getLotto(Integer number){
       List<? extends Lotto> listaLotto;
       if(number==1) {
            listaLotto=new ArrayList<LottoImplFirst>();
       }
       else if(number==2) {
            listaLotto=new ArrayList<LottoImplSecond>();
       }
       return listaLotto;
    }
}

Upvotes: 0

Santosh
Santosh

Reputation: 17893

Try this. I just compiled it:

class MyClass {
   public List<? extends Lotto> getLotto(Integer number){

       List<? extends Lotto> listaLotto = null; 

       if(number==1) {
           listaLotto= new ArrayList<LottoImplFirst>();
       }           
       else if(number ==1) {
           listaLotto=new ArrayList<LottoImplSecond>();
       }
   return listaLotto;
}
}

Upvotes: 0

MByD
MByD

Reputation: 137312

If you return List<Lotto>, it doesn't matter if inside you use a specific type or not, and actually, you can't do it, as ArrayList<LottoImplFirst> is NOT a subclass of List<Lotto>. You can actually do:

public List<Lotto> getLotto(Integer number){
   return new ArrayList<Lotto>();
}

(Also note that you declared the variables (listaLotto) inside the if-else statement, so the return statement is out of scope for them.

Upvotes: 2

pap
pap

Reputation: 27614

public interface Lotto { }

public class LottoImplSecond implements Lotto { }
public class LottoImplFirst implements Lotto { }

public class MyClass {
   public List<? extends Lotto> getLotto(Integer number){
       List<? extends Lotto> listaLotto;
       if(number==1) listaLotto=new ArrayList<LottoImplFirst>();
       else if(numeber==1) listaLotto=new ArrayList<LottoImplSecond>();
       return listaLotto;
    }
}

Upvotes: 12

Related Questions