What is chained inheritance?
Chained methods are class methods that return the instance of the object so you can call another method on the same object. This article looks at the problems you can face with implementing classes with chained methods in Java when using inheritance, and how to solve them.
What is method chaining in C++?
Method chaining (Wikipedia) is a feature which allows you to chain method calls one after another without repeatedly specifying variable the method is called on. Its primary goal is to reduce unnecessary repeated names, improving code readability.
How are fluent interfaces related to method chaining?
A fluent interface is an object-oriented API that depends largely on method chaining. The goal of a fluent interface is to reduce code complexity, make the code readable, and create a domain specific language (DSL). It is a type of method chaining in which the context is maintained using a chain.
What happens when Method chaining is not used?
When method chaining is not used, I can always know that a method call operates on something related to the result of the previous call – with chaining, this assumption breaks, and I have to semantically process the whole chain to understand what the actual object being called really is. For example:
Why is it not good practice to chain objects?
Someone casually reading the code might not catch that it would be an implicitly returned object in the chain rather than the original reference. Chaining different objects can also lead to unexpected null errors. In my examples, assuming that foo is valid, all the method calls are “safe” (e.g., valid for foo). In the OP’s example:
Why is method chaining a good practice in OOP?
Method Chaining is particularly effective with grammars like parent::= (this | that)*. The use of different methods provides readable way of seeing which argument is coming next. Similarly optional arguments can be easily skipped over with Method Chaining.