Java 8 - Wildcard extends with BiPredicate not working [duplicate]












1















This question already has an answer here:




  • Difference between <? super T> and <? extends T> in Java [duplicate]

    15 answers




I have no idea why it's not working.



Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)



import java.util.function.BiPredicate;

public class PredTest {

public static void main(String args) {

class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};

class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};

BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();

// ########### I see error in the below line
System.out.println(tester.test(f, m));

}

}









share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
    – BackSlash
    Nov 22 at 10:33












  • PECS strikes again...
    – Eugene
    Nov 22 at 10:33






  • 1




    @BackSlash or indeed just BiPredicate<Fruit, Fruit>.
    – Michael
    Nov 22 at 10:45
















1















This question already has an answer here:




  • Difference between <? super T> and <? extends T> in Java [duplicate]

    15 answers




I have no idea why it's not working.



Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)



import java.util.function.BiPredicate;

public class PredTest {

public static void main(String args) {

class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};

class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};

BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();

// ########### I see error in the below line
System.out.println(tester.test(f, m));

}

}









share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
    – BackSlash
    Nov 22 at 10:33












  • PECS strikes again...
    – Eugene
    Nov 22 at 10:33






  • 1




    @BackSlash or indeed just BiPredicate<Fruit, Fruit>.
    – Michael
    Nov 22 at 10:45














1












1








1








This question already has an answer here:




  • Difference between <? super T> and <? extends T> in Java [duplicate]

    15 answers




I have no idea why it's not working.



Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)



import java.util.function.BiPredicate;

public class PredTest {

public static void main(String args) {

class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};

class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};

BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();

// ########### I see error in the below line
System.out.println(tester.test(f, m));

}

}









share|improve this question














This question already has an answer here:




  • Difference between <? super T> and <? extends T> in Java [duplicate]

    15 answers




I have no idea why it's not working.



Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)



import java.util.function.BiPredicate;

public class PredTest {

public static void main(String args) {

class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};

class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};

BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();

// ########### I see error in the below line
System.out.println(tester.test(f, m));

}

}




This question already has an answer here:




  • Difference between <? super T> and <? extends T> in Java [duplicate]

    15 answers








java generics java-8






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 10:30









Ajeetkumar

4642719




4642719




marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
    – BackSlash
    Nov 22 at 10:33












  • PECS strikes again...
    – Eugene
    Nov 22 at 10:33






  • 1




    @BackSlash or indeed just BiPredicate<Fruit, Fruit>.
    – Michael
    Nov 22 at 10:45


















  • You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
    – BackSlash
    Nov 22 at 10:33












  • PECS strikes again...
    – Eugene
    Nov 22 at 10:33






  • 1




    @BackSlash or indeed just BiPredicate<Fruit, Fruit>.
    – Michael
    Nov 22 at 10:45
















You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
– BackSlash
Nov 22 at 10:33






You should use ? super Fruit instead. What is the difference between <? super T> and <? extends T>?
– BackSlash
Nov 22 at 10:33














PECS strikes again...
– Eugene
Nov 22 at 10:33




PECS strikes again...
– Eugene
Nov 22 at 10:33




1




1




@BackSlash or indeed just BiPredicate<Fruit, Fruit>.
– Michael
Nov 22 at 10:45




@BackSlash or indeed just BiPredicate<Fruit, Fruit>.
– Michael
Nov 22 at 10:45












1 Answer
1






active

oldest

votes


















3














Suppose you changed your lambda expression to:



    BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};


Would you still expect the compiler to allow passing a Mango to this BiPredicate?



Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.



Changing your BiPredicate to:



BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};


will eliminate the compilation error.






share|improve this answer





















  • But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
    – daniu
    Nov 22 at 10:41






  • 1




    @daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
    – Eran
    Nov 22 at 10:50










  • But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
    – daniu
    Nov 22 at 13:12






  • 1




    @daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
    – Eran
    Nov 22 at 13:18


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Suppose you changed your lambda expression to:



    BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};


Would you still expect the compiler to allow passing a Mango to this BiPredicate?



Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.



Changing your BiPredicate to:



BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};


will eliminate the compilation error.






share|improve this answer





















  • But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
    – daniu
    Nov 22 at 10:41






  • 1




    @daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
    – Eran
    Nov 22 at 10:50










  • But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
    – daniu
    Nov 22 at 13:12






  • 1




    @daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
    – Eran
    Nov 22 at 13:18
















3














Suppose you changed your lambda expression to:



    BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};


Would you still expect the compiler to allow passing a Mango to this BiPredicate?



Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.



Changing your BiPredicate to:



BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};


will eliminate the compilation error.






share|improve this answer





















  • But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
    – daniu
    Nov 22 at 10:41






  • 1




    @daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
    – Eran
    Nov 22 at 10:50










  • But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
    – daniu
    Nov 22 at 13:12






  • 1




    @daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
    – Eran
    Nov 22 at 13:18














3












3








3






Suppose you changed your lambda expression to:



    BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};


Would you still expect the compiler to allow passing a Mango to this BiPredicate?



Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.



Changing your BiPredicate to:



BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};


will eliminate the compilation error.






share|improve this answer












Suppose you changed your lambda expression to:



    BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};


Would you still expect the compiler to allow passing a Mango to this BiPredicate?



Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.



Changing your BiPredicate to:



BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};


will eliminate the compilation error.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 10:37









Eran

278k37447533




278k37447533












  • But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
    – daniu
    Nov 22 at 10:41






  • 1




    @daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
    – Eran
    Nov 22 at 10:50










  • But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
    – daniu
    Nov 22 at 13:12






  • 1




    @daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
    – Eran
    Nov 22 at 13:18


















  • But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
    – daniu
    Nov 22 at 10:41






  • 1




    @daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
    – Eran
    Nov 22 at 10:50










  • But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
    – daniu
    Nov 22 at 13:12






  • 1




    @daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
    – Eran
    Nov 22 at 13:18
















But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
– daniu
Nov 22 at 10:41




But how can it then access nf.name, if nf could be any superclass of Fruit (eg Object)?
– daniu
Nov 22 at 10:41




1




1




@daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
– Eran
Nov 22 at 10:50




@daniu BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().
– Eran
Nov 22 at 10:50












But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12




But BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12




1




1




@daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
– Eran
Nov 22 at 13:18




@daniu yes, but then the lambda expression won't contain nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.
– Eran
Nov 22 at 13:18



Popular posts from this blog

Sphinx de Gizeh

Dijon

Guerrita