0
Hello, good afternoon, I have a question related to polymorphism.
Suppose I have an object Casa
implementing Imovel
where Imovel
has a method getValorIptu()
and House has an attribute nomeProprietario
.
I would like to have a method that only calculates the total value of the IPTUS of a Property List and tbm a method that applies, or calculates, a tax to a property.
Why can’t I send a list of homes to a method that accepts a property listing? And, since I can’t have you do what I said in the question above, because I can send a house to a method that accepts a property?
I tried to use more or less the logic that exists when making a method that accepts a List<?>
and he also accepts a ArrayList
pq the same implements List
. Likewise, Casa
implements Imovel
but makes a mistake.
Is there any other way?
The intention
is that I can create a method that is punctual in what it will use, in the case of the method that receives a list, it would only use the getValorIptu()
and would not need nomeProprietario
In the image below is the test code I did, but I still did not understand
This answers your question? If B is a subtype of A, why is a collection of B not a subtype of a collection of A?
– hkotsubo
Basically, when there are lists (and any other collection involved), things get complicated (just because
A
is a subtype ofB
, doesn’t mean thatList<A>
is a subtype ofList<B>
). In addition to the question already suggested above, which explains the problem, has this one too– hkotsubo