I have this following interface :
public interface FruitDetails {
public String getFruitName();
}
And few Classes that implements the above interface :
public class Banana implements FruitDetails{
private int index;
public Banana(int i) {
index = i;
}
@Override
public String getFruitName() {
return "Banana Fruit " + index;
}
}
and
public class Apple implements FruitDetails{
private int index;
public Apple(int i) {
index = i;
}
@Override
public String getFruitName() {
return "AppleFruit " + index;
}
}
I have a function that prints the details of the fruits :
static void getFruitDetails(List<FruitDetails> fruits) {
for (FruitDetails fruitDetails : fruits) {
System.out.println(fruitDetails.getFruitName());
}
}
Now i will create a list of Bananas :
List<Banana> bananaList = new ArrayList<Banana>();
bananaList.add(new Banana(0));
bananaList.add(new Banana(1));
Now i want to print Details of bananas in the above list using getFruitDetails.
Issue is if try to call
getFruitDetails(bananaList);
I am getting compile time error :
The method getFruitDetails(List<FruitDetails>) in the type MainClass is not applicable for the arguments (List<Banana>)
How I can resolve this.
And also need to define getFruitDetails such that i should be able to print details of banana list or apple list