Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the below statement: -</p> <pre><code>JungleCat cat1 = new JungleCat(); </code></pre> <p>You can break it up in two parts: -</p> <pre><code>JungleCat cat1; // This creates a reference of type `JungleCat` cat1 = new JungleCat(); // This creates an object of type `JungleCat`. </code></pre> <p>Now, you are making the <code>cat1</code> reference, point to the <code>JungleCat</code> object. References are nothing but a link to the object that is created, so that you can access them.</p> <p>You can also create object just like this: -</p> <pre><code>new JungleCat(); // Will create an unnamed object </code></pre> <p>But, in the above case, you will only be able to use the methods and properties at the place where you have instantiated. But, later on, since you have no reference to access that object, you can't access it's properties either.</p> <hr> <p>Now, let's move ahead to the 2nd statement: -</p> <pre><code>Cat cat = new JungleCat(); </code></pre> <p>Here, as you can guess, you have reference of type <code>Cat - Super Class</code>, and an object of type <code>JungleCat</code>. This is what we call <code>Polymorphism</code>.</p> <p>So, basically, you can create a reference of any super type, and make it point to an object of any subtype. This is pretty simple to understand - <em>"Since a JungleCat is a Cat only. So, you can always have a Cat reference point to JungleCat".</em></p> <p>This is not true the other way round. For e.g.: -</p> <pre><code>JungleCat ref = new Cat(); </code></pre> <p>Now this is not valid. Because a <code>Cat</code> is not neccessarily a <code>JungleCat</code>. It can be any other Cat. So, you can't have your <code>JungleCat</code> reference point to a <code>Cat</code> object.</p> <hr> <p>Now here's your <strong>actual concern</strong>: -</p> <blockquote> <p>I'm wondering what are the object types of cat1, cat2, cat3, cat4, and cat5</p> </blockquote> <p>Well, <code>cat1</code>, <code>cat2</code>.. are not objects, but are references pointing to some objects. And you can infer from the above explanation, the reference type of each of them.</p> <p>Object types are the type used on the RHS of the object creation statement. The type used with <code>new</code> keyword, is the type of the <code>Object</code>. You can have different types of reference pointing to the same object type. </p> <p>So, you can have both <code>cat1</code>, and <code>cat2</code> references pointing to the same object type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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