Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method with interface return type

 
diwakar k.
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is written in K&B that a method can return interface implementer by citing the following snippet:


Myquestion is when interfaces are not part of the object's inhertance tree,how could a return type (here an object of class Gum) be returned?? In otherwords is a return type of implementer be returned??

Edited by Corey McGlone: Added CODE Tags
[ August 02, 2004: Message edited by: Corey McGlone ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diwakar k.,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by diwakar k.:
Myquestion is when interfaces are not part of the object's inhertance tree,how could a return type (here an object of class Gum) be returned?? In otherwords is a return type of implementer be returned??


There is no requirement that says that an object to be returned from a method need be in the inheritance tree of the class in which the method is defined. In this case, a new object will be created that implements the interface. That object is then returned. There is no need for that interface to be a part of the inheritance tree of your class - the interface needs only be accessible from this class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic