• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inteface return type

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the method can return a class which implements this Interface.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will return the instace Of the classes who implemented that interface.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"chandru K",
Please check your private messages regarding an important administrative matter.
-Ben
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I was making sure people checked the API.




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