-1

This is pretty trivial question, but couldn't find precise answer on SO, can anybody explain what happens in the memory when I declare a variable like:

Animal obj= new Horse();

If you see above declaration type(Animal) object is different and initialisation(Horse). What happens behind the scene.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
rampuriyaaa
  • 4,926
  • 10
  • 34
  • 41
  • 5
    Nothing. The JVM doesn't carry type information like that. The information is held solely in the compiler. Note that in C++, there would be a different answer. – Falmarri Feb 20 '17 at 05:03
  • 4
    @Falmarri You have that exactly back to front. The information is solely in the compiler in C++, leaving aside RTTI for the moment. The JVM carries type information in the bytecode. – user207421 Feb 20 '17 at 05:08
  • This [other answer](http://stackoverflow.com/a/20097325/697630) appears somewhat related. – Edwin Dalorzo Feb 20 '17 at 05:17
  • 1
    To be clearer: the information that `obj` is declared as `Animal` isn't carried around in the object itself. The object looks exactly the same whether you say `Animal obj = new Horse()` or `Horse obj = new Horse()`, which is what I think the question was trying to ask. You can retrieve the declared type of `obj` by using reflection, though. My C++ is rusty, and there may be a difference in how the objects look in those two cases, since `obj` would be the actual object data and not a reference as in Java. But I don't remember. – ajb Feb 20 '17 at 05:47
  • @EJP My comment about C++ was related to slicing. – Falmarri Mar 01 '17 at 02:22

4 Answers4

1

Animal obj= new Horse();

you're constructing an object obj that can do the job of either a Horse or a Animal. Horse is inherited from Animal. This way you can access all the properties and methods of Animal.

And found this here This is the basis for polymorphism: Imagine you have several child classes that inherit from you parent class. You want to use all these child classes through the interface / methods defined on your parent class, without worrying about the implementation details in each child class (each might do something different, but with the same overall semantics).

This is possible because the child class has a IS-A relationship with its parent class since child inherits from parent.

I'd suggest you to learn Inheritance & Polymorphism:

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
sai
  • 419
  • 2
  • 7
  • 17
  • Supposing that a `Horse` is indeed an `Animal`, which is not evident in the question because the OP did not share their definitions. Most likely that's the case. – Edwin Dalorzo Feb 20 '17 at 05:24
1

When ever you use new keyword you create an object. Objects are created in heap. new Horse() will create an object of Horse in the heap and will do initialization as per the class Horse.java.

Now with the case presented assuming Horse inherits from Animal. Referencing the object of Horse by a variable of type Animal(Animal a = new Horse() ) does not affect the object creation ( it is created same even if you do Horse h = new Horse() ). It only restricts the invoking of the additional method present in the class Horse using variable of super class type. You need to cast it if you wish to invoke the methods of class Horse. But point to take care is that we can invoke whats there in the object created by the underlying blueprint class. Thats why if you override a method then it doesnt matter if you have a variable of super type or if you cast a variable of Horse to Animal or you simply use variabl of type Horse to reference an obkect of Horse, invoking a method which is overrided will always result in the overriden behaviour.

These are the things we as a programmers must understand, rest should be left to the JVM implementation. How it woks internally may change on different platforms but the contract and end result on top level is always the same.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • 1
    You should probably say "as per the class Horse" and not "as per the class Horse.java" since "Horse.java" is not a class, but a source code file. It may contain class definitions, but it is not the class itself. Also, we cannot be certain as per the question if `Horse` was indeed defined in that file. – Edwin Dalorzo Feb 20 '17 at 05:33
  • @Edwin Dalorzo , you are right, i edited the answer thanks for pointing out – nits.kk Feb 20 '17 at 08:38
-1

When you declare like that then object will be created for Horse class and it will create in heap memory. Hope you find the enough answer. :) cheers !

-1

this is polymorphic in java, understand? accurately,this is also called Dynamic Binding.I think , as a newbie,we just konw how to use is ok,I am new to OS , please fogive my chinese english=_=

李鹏程
  • 11
  • 2
  • I don't think this really answers the question. Just giving terms ("polymorphic" and "dynamic binding") doesn't really help someone understand. – ajb Feb 20 '17 at 05:44
  • @李鹏程 its ok. Actually we must know how to use it and to know that you need to know the concept well. How its done by JVM doesnt matter much as its platform dependent. Contract set by JLS is what matters. – nits.kk Feb 20 '17 at 16:49
  • @nits.kk thanyou! can you explain what is Contract set by JLS?,I know JLS is the abbreviation of java language specification=_=? – 李鹏程 Feb 22 '17 at 02:59