Why wont this assignment work?

前端 未结 5 1944
慢半拍i
慢半拍i 2021-01-28 00:04

I\'ve got a class Results which extends ArrayList. I\'ve got an object i which has a function i.getResults() w

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 00:18

    Well, you're trying to assign an object of a superclass type to a reference of a subclass type, and of course that's a compile-time error, because it might not be valid. It's the same as if you tried to assign the return value of a method that returns Object to a variable of type String. If you are absolutely sure the getResults() method returns a Results object, you should change its return type. Or if you're only sure of it in this context, you can use a cast.

提交回复
热议问题