3

I have two classes as given below.

    class One {
      void disp() {
         System.out.println("Super class disp() method");
      }

      void show() {
        System.out.println("Super class show() method");
      }
    }

    class Two exntends One{
      void disp(){
          System.out.println("Sub class disp() method");    
      }
    }

class TestInheritance {
   public static void main(String args[]) {
     One o = new Two();
     o.disp();

     Two t = (Two) new One();
   }
}
  1. What happens in terms of memory when we convert sub class object to super class object.?

    One o = new Two();
    
  2. What happens in terms of memory when we convert super class object to sub class object.?

    Two t = (Two) new One();
    
  • 1
    You may want to take a look to http://stackoverflow.com/questions/840322/how-does-the-java-cast-operator-work – Javier Diaz Apr 29 '14 at 11:15
  • 1
    Note: `One o` and `new Two()` are references to Objects. When you cast, you are changing the type of the *reference* and the object is not altered/copied/translated. – Peter Lawrey Apr 29 '14 at 11:28

4 Answers4

7

What happens in terms of memory when we convert sub class object to super class object.?

One o = new Two();

What happens is that a Two instance is created, and the reference is assigned to o.

This doesn't change the Two instance in any way. It is not "converted". It remains a Two.

What happens in terms of memory when we convert super class object to sub class object.?

Two t = (Two) new One();

What happens is that a One instance is created, and its type is tested to see if the One is a Two. Since it isn't ... a ClassCastException is thrown immediately.

(Following that, the One instance that was just allocated will be unreachable and a candidate for garbage collection ... the next time that the GC runs.)

In fact, you probably meant this:

One o = new Two();
Two t = (Two) o;

What happens in this case is that the first statement creates a Two instance and assigns its reference to o. Then in the second statement, the type of the object refered to by o is tested to see if it "is-a" Two. Since it is ... the reference is then assigned to t.

Once again, this doesn't change the Two instance in any way. It is not "converted". It remains a Two.


Can you give some detail about how the memory will be allocated.? I mean "when Two class object is created, memory will be allocated for Two's members and One's members as well (reason being One is super class for Two)" and will be referred by 't'.

That is correct

When we do "One o = new Two()", we will be able to access only One class members.

That is correct.

When we are converting like this (sub class object pointed by super class reference), what exactly happens internally..?

All that happens is that the compiler & runtime "forget" that o actually refers to a Two, and only permits you to use the reference to access the fields / methods defined by the One class.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • when One o = new Two(); is executed, Two instance will be created. In that case memory will be created for Two class methods. If the object is not converted, how we are able to access o.disp() and o.show().? –  Apr 29 '14 at 12:25
  • Because every Two instance inherits the methods and fields defined by the One class. – Stephen C Apr 29 '14 at 14:34
  • Can you give some detail about how the memory will be allocated.? I mean "when Two class object is created, memory will be allocated for Two's members and One's members as well (reason being One is super class for Two)" and will be referred by 't'. When we do "One o = new Two()", we will be able to access only One class members. When we are converting like this (sub class object pointed by super class reference), what exactly happens internally..? –  May 12 '14 at 13:15
  • the last case you explained is called Object Slicing. – Rupesh Yadav. Jun 13 '16 at 11:37
1

Casting doesn't change anything its just a way of treating object of one type as object of some other (allowable) type.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

First of all your 2nd condition Two t = (Two) new One(); is not possible because subclass object can not hold the object of super class.

In case of memory JVM treat as method overriding in case of disp() method so One o = new Two() holds the properties of super class One as well as subclass Two.

Method are belonging to method area of the particular class

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

anCasting doesn't do anything, it just tells the compiler that object of type A is actually of object of type B, and therefore I can call the methods of object B on it. At compile time, all the casting is erased, you just better hope that the method call to the object actually succeeds, otherwise you will get a run time error of some description.

phil_20686
  • 4,000
  • 21
  • 38