2

I was going with the concept of upcasting and downcasting in java, which are also referred as widening and narrowing.

  • UpCasting (Widening) happens automatically from derived class to base class. i.e if it has a is-a relationship.
  • Downcasting has to be done explicitly for run time check.

Okay, I got the concept and everything. But, how its working in this case?

public class ObjPair {
    Object first;
    Object second;

    public ObjPair(Object first, Object second) {
        this.first = first;
        this.second = second;
    }

    public Object getFirst() {
        return first;
    }    
    public Object getSecond() {
        return second;
    }

    public static void main(String[] args) {
        ObjPair objPair = new ObjPair("A",2.2); // Goes widning conversion
        System.out.println(objPair.getFirst());
        System.out.println(objPair.getSecond());
    }
}

ObjPair objPair = new ObjPair("A",2.2);

  • This is going through upcast, String to object and Double to object and the state gets store in the objPair. Great..!!!

Now,when i do objPair.getFirst() and objPair.getSecond(). It returns me A and 2.2.

  • How does it remember the string and double, widening/upcast is supposed to remember the super-class states and methods.
  • How is it able to access sub-class types and values?
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Ritt
  • 3,181
  • 3
  • 22
  • 51
  • Well, in your `ObjPair`, it is stored as a string and a double. – Turtle Aug 02 '17 at 09:17
  • It is getting stored as Object after upcast, isn't it? That is where upcast is happening. – Ritt Aug 02 '17 at 09:19
  • 3
    Also, Object class has a toString() method which println() will use. String and Double (autoboxed from double) override toString() to print their values properly. – KC Wong Aug 02 '17 at 09:20
  • The code abowe really does not "access sub-class types". It only use a polimorphism. – Zefick Aug 02 '17 at 09:26
  • @Ritesh I added an example of what I said if you want a more visual example, to complement RealSkeptic's answer. – Turtle Aug 02 '17 at 09:51

4 Answers4

6

Casting of object references does not change the object. It simply allows assigning it into a reference of a different type. The object itself remains the same.

In your case, it needs two Object references, it checks compatibility (no problem there), and then the references are set in variables of type Object. The instances themselves do not change. If they have methods that override those of Object, then the overriding methods will be called.

Thus, when it comes to the part where it prints the object, it simply uses String.valueOf, which calls the object's toString() method. The instance accessed from the Object variables is actually a String object, and String overrides toString() to return itself. Double also overrides toString. These overrides are called, as the instance is still an instance of String and an instance of Double. Only the reference is Object.

Note that you also have a cast from double to Double there. This implicit boxing does change the object - it takes a primitive and creates a new Double from it.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
2

If you simply test this code:

public class Main {

    public Object first;
    public Object second;


    public static void main (String[] args){
        Main a = new Main();
        a.first = new String("foo");
        a.second = 5;

        System.out.println(a.first.getClass().toString());
    }
}

It outputs class java.lang.String. You can see that it isn't stored as an Object. This is achieved through the use of metadata.

Turtle
  • 1,626
  • 16
  • 26
1

Keep in mind: the object in memory is what it is. If you create a Double object, then it is a Double object resembling the numerical value.

The fact that the variable that holds the reference uses a super type doesn't affect the object referenced to at all!

In your example, the auto-boxing create a Double object under the cover, andgetSecond() returns a reference to that Double object.

That is all there is to this.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Okay so here it is, lets take an example. You have a big square box,named Object and another small box. If you keep this small box inside your big box,all the properties of small box and big box are in the big box. Then inside the small box,there are two sticks,you are labeling the first stick as 'A' and second as '2.2'. Now the big box can see what is inside of small box. Now for that instant, the small box is having two sticks and labelled as the way they are. (Rememeber the Object class is always the super class/parent of every classes in java).

Amal lal T L
  • 400
  • 5
  • 20
  • That's correct, the big-box cannot see what is implemented inside small box, unless the properties are same. In my case, it takes toString() implementation of small boxes (double and string). – Ritt Aug 02 '17 at 09:35