Search code examples
javaobjectlambdasuperclass

Java lambda: Expected Object but found MyClass


If I have:

public class MyClass{
    MyClass(Object b){
        //Some code here
    }
}

And I do:

MyClass X = new MyClass(new SomeOtherClass());

It works just fine, im assuming, because every class has Object as a superclass.

But, if I do:

import java.util.function.Predicate;
public class MyClass{
    MyClass(Predicate<Object> b){
        //Some more code here
    }
}

And then:

MyClass X = new MyClass((SomeOtherClass s) -> {
    //Even more code
    return true;
});

I get an error saying:

Incompatible parameter types in lambda expression: expected Object but found SomeOtherClass

Shouldn't I be able to send a SomeOtherClass object in that lambda expression? I'm using the Object class because i wanna be able to recieve not only SomeOtherClass, but also SomeOtherClassXPTO. I tried looking this up and found nothing so far, so my apologies in advance if this has been asked before.


Solution

  • Well, when you defined your predicate as Predicate<Object> it's restricted to only accept objects which getClass() method returns Object.

    For this method to work you would be doing something like Predicate<? extends Object> but this does not make much sense. Use Predicate<?> alone then.

    Otherwise, another way to solve this is to find the common denominator of your two classes, probably a parent class and use Predicate<? extends ParentClass>.

    A final solution would be to create, depending on your application logic, an interface and implementing the interface by both objects to be able to something like the following Predicate<? extends YourInterface>