Thanks and Regards
Thanks and Regards
If you worry you cannot work... If you work you need not worry
the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??
Thanks and Regards
Kumar Biplav wrote:Hi,
I have a question regarding the explanation of first option(A) which says ,"The return type of process is definitely declared as a List, not an ArrayList" but we can return an ArrayList to a method which has its return type declared as List,right?
The return type isn't part of the method signature.Tyoma Sakurakoji wrote:. . . according to the method signature
Say supertype, not superclass; java.util.List isn't a class. That is an example of inheritance, not of polymorphism.it will be "downgraded" to super class due to polymorphism nature.
That is the right explanation. In real life, you would declare your ArrayList as List:-\you can change line 6 to read words = new LinkedList<>(); and nothing strange will happen.So, a returned ArrayList would be returned as List. To make it back to an ArrayList you'll need to perform an explicit cast. That's why an option A is not correct . . . .
What a show! What atmosphere! What fun! What a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|