The term "method chaining" I believe is usually reserved for the case where each method returns the same object that was originally called upon. Take a look at StringBuffer for an example. Otherwise, if each method returns a different object, I think there's no special name for it.It doesn't really matter what you call it as they both work the same way in the end.
Originally posted by Jayashree M: Suppose we have something as follows:
How does the evaluation take place, if say suppose all three methods return a String?
best way to find out is to try it out... but to help you answer your question... the object that is returned by obj.method1() will require method2() as one of it's methods, and the object that is returned by method2 will require that method3() is one of its methods... otherwise it will not compile...
an example to look at...
just comment out
to get the example to compile... and you can see how it evaluated...
This kind of chaining is sometimes discoraged when you're designing your own classes. Here's a story from the Portland Pattern Repsository ...
"If you want your dog to run, do you talk [to] your dog or to each leg? Further, should you be able to manipulate the dog's leg without it knowing about it? What if your dog wants to move its leg and it doesn't know how you left it? You can really confuse your dog."
The moral: Change the state of a contained object only through the containing object's interface.
"Aha!" you say. "If I employ the technique of encapsulation, then a method won't even know of the existence of any contained objects."
That's precisely the point. To walk your dog, you don't need to know of the existence of its legs. (And in fact, dogs with fewer than four legs can go for walks. There is even a case of a dog whose hindquarters rested on a little carriage; its "walk" method employed two legs and two wheels (these latter, one hopes, encapsulated within a "carriage" interface).)
When you say you know method1 returns an object that has method2, and method2 returns an object that has method3 you start to write The Class That Knew Too Much. Let us know if this kind of discussion sounds interesting. We got a million of em. [ February 15, 2005: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 19, 2005
Hello Everybody Thanks a lot for the information. I was going thru a few legacy coe at the place I Work and they a lot of such method calls...I couln't figure it out initially. Now it is clear and I know how to go about it. Thanks again