-5
class Person {
    public Person(){}
}
class Employee extends Person{
    public Employee() {}
}
class Manager extends Employee{
    public Manager() {}
}
public class HeapObjectTest {
    public static void main(String[] args) {
        Manager manager = new Manager();}
}

How many object will create on the Heap for Above code?

trincot
  • 317,000
  • 35
  • 244
  • 286
Panky031
  • 425
  • 1
  • 5
  • 14
  • 10
    The JVM will create around 10,000 objects just getting to the main() method, one more isn't going to make much difference. Note: loading a class for the first time creates lots of objects. – Peter Lawrey Jun 21 '16 at 17:40
  • 1
    Possible duplicate of [Object Creation Logic in Java](http://stackoverflow.com/questions/26115207/object-creation-logic-in-java) – sauumum Jun 21 '16 at 17:53
  • 2
    This is a homework question, right? You should forget about the heap, that's a complete red herring. Just think about how inheritance works. – biziclop Jun 21 '16 at 17:53

3 Answers3

1

If we talk about only your code, then there is only one Manager object, and there will be constructor-chaining till Object class. Apart from this object there will be other objects also which are needed by JVM to run your program, these will be class objects, method objects which are currently loaded in to run your program.

For more details about the execution order of any program, to get more understanding, please read once below link,

https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.1

pbajpai
  • 1,303
  • 1
  • 9
  • 24
0

Invoking this line of code:

Manager manager = new Manager();

Will create one object, an instance of the Manager class. The implementation of the Manager class borrows from the implementation of the Employee and Person classes - however, just because you inherit from those classes, doesn't mean that they get treated as separate objects.

antiduh
  • 11,853
  • 4
  • 43
  • 66
0

Your program will create only one Object for Manager class. Manager class extends the properties/behavior of Employee and Person but it will create object for only Manager and not for others.

As per basic definition of Object- it is an instance of the class and class is just a blue print on how the object should be created. Hope this helps.

Ravinder G
  • 36
  • 5