This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

arrayList  RSS feed

 
manohar reddy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the return type of a method is �Object�, can we return an ArrayList from that method.


If the return type of a method is ArrayList, can we return an Object of type Object class from that method.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the return type of a method is �Object�, can we return an ArrayList from that method.

Yes, because ArrayList extends Object.


If the return type of a method is ArrayList, can we return an Object of type Object class from that method.

No, because Object doesn't extend ArrayList.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note this rule.

1. If return type of a method is A, that method can return object of A or object of any type which is subclass of A. It can not return instance of any other class.

It also applies to method parameters.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manohar reddy:
Another homework question.


Manohar - If all of these very abstract questions aren't homework, please give us some context.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!