Classes, Objects, Inheritance?
<p>I'm supposed to create three classes: a parent, and child 1 and child 2.<br>
Child 1 and 2 are supposed to inherit from the Parent class. 
So I believe I've done that. </p>

<pre><code>class Parent:
    """Parent Object"""

    def __init__(self):
        self.greeting = "Hi I'm a Parent Object!"


class ChildA(Parent):

    def __init__(self):
        childclass.__init__(self)
        self.childgreeting = "Hi I'm a Child Object!"


class ChildB(Parent):
    pass
</code></pre>

<p>Now I have to write a parent object and the children objects which will print out their respective strings.<br>
That's where I'm getting confused: I already put in the strings that they are a child or parent object within their classes.<br>
But how do I get them to print as an object?</p>

<p>I've started out my code like this. </p>

<pre><code>class Parent(object):

class ChildA(object):

class ChildB(object):
</code></pre>

<p>How to get those strings to print is bugging me.<br>
And I have a feeling that my ChildA code for the class is not correct either.</p>

<p>Can anyone help me?</p>

<blockquote>
  <p>Child 1 and 2 are supposed to inherit from the Parent class. So I believe I've done that</p>
</blockquote>

<p>Yes, in the first code, you have, but not in the second code. </p>

<blockquote>
  <p>I have to write a parent object and child 1 and 2 objects that will print out their respective strings</p>
</blockquote>

<p>Okay... </p>

<pre><code>p = Parent()
child_a = ChildA()

print(p.greeting) 
print(child_a.childgreeting)
</code></pre>

<p><strong>However</strong> - <code>ChildA()</code> won't work because <code>__init__</code> should look like this </p>

<pre><code>class ChildA(Parent):

    def __init__(self):
        super().__init__() # This calls the Parent __init__
        self.childgreeting = "Hi I'm a Child Object!"
</code></pre>

<p>Now, the above code will work. </p>

<p>But, I assume you want the <code>greeting</code> attribute to be overwritten? Otherwise you get this</p>

<pre><code>print(child_a.greeting) # Hi I'm a Parent Object!
print(child_a.childgreeting) # Hi I'm a Child Object!
</code></pre>

<p>If that is the case, you simply change <code>childgreeting</code> to <code>greeting</code>. Then, from the first example</p>

<pre><code>print(p.greeting) # Hi I'm a Parent Object!
print(child_a.greeting)  # Hi I'm a Child Object!
</code></pre>

<blockquote>
  <p>how do I get them to print as an object?</p>
</blockquote>

<p>Not entirely sure what you mean by that, but if you define <code>__str__</code> to return <code>greeting</code></p>

<pre><code>class Parent:
    """Parent Object"""

    def __init__(self):
        self.greeting = "Hi I'm a Parent Object!"

    def __str__(self):
        return self.greeting
</code></pre>

<p>The example now becomes </p>

<pre><code>print(p) # Hi I'm a Parent Object!
print(child_a)  # Hi I'm a Child Object!
</code></pre>

python class object inheritance