Understanding Object.clone() in Java
<p>I know that using this cloning mechanism is not a good idea (since it is 'broken' as some authors suggest), although I need help understanding how it works. We're given the following class hierarchy:</p>

<pre><code>class N implements Cloneable{
    protected int num;
    public N clone() throws CloneNotSupportedException{
        return (N)super.clone();
    }
}

class M extends N{
    protected String str;
    public M clone() throws CloneNotSupportedException{
        M obj = (M)super.clone();
        obj.setString(new String(this.str));
        return obj;
    }
    void setString(String str){
        this.str = str;
    }
}
</code></pre>

<p>Since <code>N</code> extends <code>Object</code>, how does the <code>super.clone()</code> return an instance of <code>N</code>? <code>super.clone()</code> is actually <code>Object.clone()</code> which returns a reference to an object of class <code>Object</code>. Why are we then able to cast it to <code>N</code>? <code>N</code> has a member <code>num</code> that is not in the class <code>Object</code>. How does the default behavior actually manage to automatically clone this variable (since it has no record of it in class <code>Object</code>)?</p>

<p>Also, the same goes for M. In <code>M.clone()</code> we're casting an object from class <code>N</code> (returned by <code>super.clone()</code>) to an object of class <code>M</code>. I know that all of this is valid, yet I do not understand why.</p>
