Java interfaces and types
<p>Let's say you have some Java code as follows:</p>

<pre><code> public class Base{
    public void m(int x){
    // code
    }
 }
</code></pre>

<p>and then a subclass Derived, which extends Base as follows:</p>

<pre><code> public class Derived extends Base{
    public void m(int x){ //this is overriding
      // code
    }

    public void m(double x){ //this is overloading
      // code
    }
 }
</code></pre>

<p>and then you have some declarations as follows:</p>

<pre><code>Base b = new Base();
Base d = new Derived();
Derived e = new Derived();

b.m(5); //works
d.m(6); //works
d.m(7.0); //does not compile
e.m(8.0); //works
</code></pre>

<p>For the one that does not compile, I understand that you are passing in a double into Base's version of the m method, but what I do not understand is... what is the point of ever having a declaration like "Base b = new Derived();" ?</p>

<p>It seems like a good way to run into all kinds of casting problems, and if you want to use a Derived object, why not just go for a declaration like for "e"?</p>

<p>Also, I'm a bit confused as to the meaning of the word "type" as it is used in Java. The way I learned it earlier this summer was, <strong>every object has one class</strong>, which corresponds to the name of the class following "new" when you instantiate an object, but <strong>an object can have as many types as it wants</strong>. For example, "e" has type Base, Derived, (and Object ;) ) but its class is Derived. Is this correct?</p>

<p>Also, if Derived implemented an interface called CanDoMath (while still extending Base), is it correct to say that it has type "CanDoMath" as well as Base, Derived, and Object?</p>
