Instance variable in java and c++
<p>as I understood, In java all the variables are refernces.</p>

<p>On the next code:</p>

<pre><code>class Parent { 
    public int x = 12; 
} 
class Child extend Parent { 
public int x = 42; 
} 
Parent p = new Parent(); 
System.out.println(p.x); 
12 
Child c = new Child(); 
System.out.println(c.x); 
42 
p = c; // be careful here! 
System.out.println(p.x); 
12
</code></pre>

<p>I think that there is no Slicing here, because we talk about refernces. For that reason, I don't understand why <code>"12"</code> is printed. <code>p</code> is now points to the area of <code>c</code>, and <code>c.x</code> is 42.</p>

<p>The same is happening in the next code on C++:</p>

<pre><code>class Parent { 
  public:
    void example () { cout &lt;&lt; "Parent" &lt;&lt; endl; } 
}; 
class Child : public Parent { 
  public: 
    void example () { cout &lt;&lt; "Child" &lt;&lt; endl; } 
};

Parent * p = new Parent(); 
p-&gt;example() 
Parent 

Child * c = new Child(); 
c-&gt;example() 
Child 

p = c; // be careful here! 
p-&gt;example() 
Parent
</code></pre>
