-1

I have the next problem with inheritance in Objects:

-First Class:

public class LegalPersonRest extends LegalPerson {}

-Second Class:

public class Company {

private List<LegalPerson> legalPersons;

}

-Third Class:

public class CompanyRest extends Company {

@Override
public List<LegalPersonRest> getLegalPersons() {
    return super.getLegalPersons();
}

}

In this method getLegalPersons I got the error: attempting to use incompatible return type.

I don't know why if LegalPersonRest extends LegalPerson.

The classes Company and LegalPerson are my domain. (layer domain)

The classes CompanyRest and LegalPersonRest extends the classes of the domain. (layer adapter)

If I change of List to List, I think is wrong because I need used LegalPersonRest.

What's the correct form of inheritance for this case?

Regards,

YanOner
  • 13
  • 8
  • I think we get this question most days: https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po – Tom Hawtin - tackline Mar 27 '20 at 04:30

1 Answers1

0

I'm just going to point out in case this was the issue: the super class needs to have the method you are overriding. Company doesn't have the method getLegalPersons(), so you can't override it. But I think Company does have getLegalPersone() because you call that method later (please produce a minimal, reproducible example). The next thing I can think of is that Company's getLegalPersons() method doesn't return a List<LegalPersonRest>. It might return a list of something else, or another object entirely. Please check the return type to see if it is the same return type as the method in CompanyRest.

nishantc1527
  • 376
  • 1
  • 12