Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible types with inheritance Java [duplicate]

I'm trying to wrap my head around this inheritance problem I'm having. Here's my current situation:

enter image description here

So I have a planning that can be either a list of activities (class Activities) or a list of things to do (class Todos).
However, if I do this in Todos:

private List<Todo> todos;

public List<Activity> getPlanning(){
    return todos;
}

It says that the types are incompatible.

  • Why are they incompatible?

Since Todo extends from Activity, aren't we certain that Todo provides at least the same functionality as Activity?

and perhaps more importantly:

  • How do I adjust my design to make things work?

(I'm not an expert in UML so forgive me if there are some mistakes in my diagram)

like image 706
Aerus Avatar asked Mar 12 '26 06:03

Aerus


1 Answers

Welcome to the nightmare and poor implementation of generics in Java. Try using public List<? extends Activity> getPlanning().

like image 147
Augusto Avatar answered Mar 13 '26 19:03

Augusto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!