Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inteface return type

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method can have a return type as an Interface.But question arises what the method will return.Though the return type is an interface definately method cannot create inteface object.

What method returns?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type determines what the implemented method must return.

Example

This means any implementing class must have a getCount() method with no parameters and an int return type. How the class does the counting is its own business.
 
Gaurav Thukral
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the method can return a class which implements this Interface.
 
chandra mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will return the implementing class of that interface.
e.g If a method is returning List it means that It is returning one of the classes which implemented list like ArrayList.......
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chandru K:
It will return the implementing class of that interface.
e.g If a method is returning List it means that It is returning one of the classes which implemented list like ArrayList.......
Oh, is that what it means. You are quite right, and I totally misunderstood the original question.



BTW: The ArrayList which is returned from the Arrays.toList() method is a different class from the ArrayList we all know and love.
 
jain ReshuR
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will return the instace Of the classes who implemented that interface.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's the beauty. it can return ANY class type, as long as that type implements the interface. this leaves the author of the method free to us any class that best fits his/her needs, or even change it later, without breaking the calling code.
 
chandra mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Oh, is that what it means. You are quite right, and I totally misunderstood the original question.



BTW: The ArrayList which is returned from the Arrays.toList() method is a different class from the ArrayList we all know and love.



campbell,

I think it is Arrays.asList() am I correct ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"chandru K",
Please check your private messages regarding an important administrative matter.
-Ben
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chandru K:

campbell,

I think it is Arrays.asList() am I correct ?

Yes you are. I think Campbell was a bit confused with the toArray methods specified by the Collection interface.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I was making sure people checked the API.




Or I was too idle to check myself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic