How is the return type Optional<? extends User> different than Optional<User> in a method?

Viewed 34

In the code below, I understand the functioning of getAgentOrConsumer1() method - it will return an object which can be either of the child type. But how is the getAgentOrConsumer2() method different?

public class Scratchpad {
  
    public static Optional<User> getAgentOrConsumer1() {

    }

    public static Optional<? extends User> getAgentOrConsumer2() {
        
    }
}

class User {

}

class Agent extends User {

}

class Consumer extends User {

}
0 Answers
Related