Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your last paragraph where you explain your reasoning is slightly incorrect.</p> <blockquote> <p>so why in the example of before i don't see the reference but directly the number? by logic, the rules of the polymorphism says that: if u have a "child" object in a "father" variable, this object, inside, remanis the same but he is used like an istance of object, so he can <strong>just uses the class object and so just the method of object, so is really strange that i don't see the reference but directly the number.</strong></p> </blockquote> <p>The beginning is correct, but the part I bolded is an incorrect conclusion you drew from it.</p> <p>You are correct that with polymorphism, the object truly remains whatever type it is, but the reference type (the type of the variable) defines what you can do with it. However, the reference type does not describe <strong>what the object does</strong></p> <p>That is the intent behind polymorphism. It is an abstraction to define what can be done separately from how it works. For example, if you have this example:</p> <pre><code>public class Vehicle { public int getWheelCount() { return 1; } } public class Car extends Parent { public int getWheelCount() { return 4; } public void blowHorn() { System.out.println("Honk honk!"); } } public class Bicycle extends Parent { public int getWheelCount() { return 2; } } Car car = new Car(); car.getWheelCount(); // 4 car.blowHorn(); //"Honk honk!" Vehicle v = new Car(); v.getWheelCount() // 4 v.blowHorn(); // COMPILE ERROR HERE! Unknown method Bicycle b = new Bicycle(); b.getWheelCount(); // 2 Vehicle v = new Bicycle(); v.getWheelCount(); // 2 </code></pre> <p>What you can conclude from this is that when over-riding a method in a sub-class, the child version is always called. A car is always a car whether you are referring to it as a vehicle or as a car. But by referring to it as a vehicle, you are limited to invoking methods which are defined on all vehicles.</p> <p>To tie it to the example, all <code>Vehicle</code> objects have a wheel size, therefore getWheelCount() is always callable whether it's Vehicle.getWheelCount() or Car.getWheelCount(). However, Car.getWheelCount() is what executes because Car over-rides it.</p> <p>If the reference type is <code>Vehicle</code>, you cannot call <code>blowHorn()</code> because that method is only available on Car.</p> <p>Going back to your example, an Integer is an Integer.</p> <pre><code>Object i = new Integer(5); i.toString(); // 5 </code></pre> <p>This prints 5 because i is an integer. The Integer class over-rides toString. The reference type (the type you are referring to the object as) only determines <strong>which</strong> methods you can call, but not which parent/child class's <strong>version</strong> of the method is called.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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