Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Inheritance defines an "Is-A" relationship.</p> <pre><code>class Point( object ): # some set of features: attributes, methods, etc. class PointWithMass( Point ): # An additional feature: mass. </code></pre> <p>Above, I've used inheritance to formally declare that PointWithMass is a Point.</p> <p>There are several ways to handle object <code>P1</code> being a PointWithMass as well as Point. Here are two.</p> <ol> <li><p>Have a reference from <code>PointWithMass</code> object <code>p1</code> to some Point object <code>p1-friend</code>. The <code>p1-friend</code> has the <code>Point</code> attributes. When <code>p1</code> needs to engage in <code>Point</code>-like behavior, it needs to delegate the work to its friend.</p></li> <li><p>Rely on language inheritance to assure that all features of <code>Point</code> are also applicable to my <code>PointWithMass</code> object, <code>p1</code>. When <code>p1</code> needs to engage in <code>Point</code>-like behavior, it already <em>is</em> a <code>Point</code> object and can just do what needs to be done.</p></li> </ol> <p>I'd rather not manage the extra objects floating around to assure that all superclass features are part of a subclass object. I'd rather have inheritance to be sure that each subclass is an instance of it's own class, plus is an instance of all superclasses, too.</p> <p>Edit.</p> <p>For statically-typed languages, there's a bonus. When I rely on the language to handle this, a <code>PointWithMass</code> can be used anywhere a <code>Point</code> was expected.</p> <p>For really obscure abuse of inheritance, read about C++'s strange "composition through private inheritance" quagmire. See <a href="https://stackoverflow.com/questions/255447/any-sensible-examples-of-creating-inheritance-without-creating-subtyping-relati">Any sensible examples of creating inheritance without creating subtyping relations?</a> for some further discussion on this. It conflates inheritance and composition; it doesn't seem to add clarity or precision to the resulting code; it only applies to C++.</p>
 

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