aspose file tools*
The moose likes Beginning Java and the fly likes Inteface return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inteface return type" Watch "Inteface return type" New topic
Author

Inteface return type

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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?


Thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Mar 20, 2008
Posts: 2
I think the method can return a class which implements this Interface.
chandra mohan
Greenhorn

Joined: May 05, 2008
Posts: 20
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: May 07, 2008
Posts: 2
It will return the instace Of the classes who implemented that interface.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
chandra mohan
Greenhorn

Joined: May 05, 2008
Posts: 20
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

Joined: Dec 11, 2004
Posts: 13410

"chandru K",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
No, I was making sure people checked the API.




Or I was too idle to check myself.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inteface return type