Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you can do it, it is a fine practice which must however be considered strongly with respect to the situation.</p> <p>Two examples:</p> <ul> <li><p>Be careful not to call methods in the class constructor: during the call your class is partially initialized, so any call to methods (of the class itself or inherited) could have bad and hard to track consequences. As a rule of thumb, the constructor must be "self-satisfied" with respect to the class.</p></li> <li><p>Since you are using methods of the class, you have free access to its implementation. Ask yourself "Do I want to access the class through its interface, or through its implementation?". </p></li> </ul> <p>To better explain the second point, suppose you have a class A with methods foo() and bar(). </p> <p>Suppose that foo() calls bar() in its code. In this case you are talking with the interface of your object. This interface could not be the one you expect! For example, suppose that the class has been reimplemented (class B, derived from A) by you, but also by an user of your code, and that method bar() has been overridden. Due to the inheritance, A::foo() will call B::bar(), instead of A::bar(). </p> <p>By not using the bar() method call, and accessing the <em>implementation</em> instead, you are guaranteed that your function behaves the same, even for child classes.</p> <p>Some of these issues enter in the so-called <a href="http://en.wikipedia.org/wiki/Fragile_base_class" rel="nofollow noreferrer">fragile base class problem</a>. Here is <a href="http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html" rel="nofollow noreferrer">a very good article</a> about it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload