How many objects are created due to inheritance in java?

前端 未结 14 1632
长情又很酷
长情又很酷 2020-11-29 18:48

Let\'s say I have three classes:

class A {
    A() {
        // super(); 
        System.out.println(\"class A\");
    }
}
class B extends A {
    B() {
             


        
14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 19:34

    Steps of object creation when you call a constructor to create object:

    1. Memory allocation using init is done. This init makes a system call to allocate memory for object creation.

    2. Then your constructor is called to initialize the object's fields.

    3. Then it calls super class constructor (If there's any super class) and Step 1 through 3 repeats.

    What you see when you decompile a class file using javap shows different calls to be made. init makes system call to initialize memory allocation but object's field are initialized when constructor's code is run.

提交回复
热议问题