Will creating an object be a parent class if we create an object of a child class?

I know that super will be called every time we create an object of a child class. But what I specifically want to know is that it will load the class or create an object of this parent class.

Thanks in advance.

+4
source share
4 answers

You have a single object.
When an instance of the class is created, it calls the first constructor of the parent class. And for each parent class, it follows the same logic: it calls the first constructor of the parent class.

Suppose: Aextends Bextends C.

new A() B, C.
C B, A , A.

, .

first .

, A extends B extends C:

class C {
    public C(){    
        System.out.println("C constructor");
    }
}

class B extends C{
    public B() {
        System.out.println("B constructor");
    }

}
class A extends B{
    public A() {
        System.out.println("A constructor");
    }
}


public class Test {
    public static void main(String[] args) {
        new A();
    }
}

Test, A main(), -verbose ( ) java:

java -verbose Test

- :

[Loaded java.lang.Object from C:\...]
[Loaded java.io.Serializable from C:\...]
... // load all classes required until loaded C, B and A classes
[Loaded C from file:/C:/...]
[Loaded B from file:/C:/...]
[Loaded A from file:/C:/...]
C constructor
B constructor
A constructor

, :

  • , (Object) (B).
  • , (Object) (B).
+2

, . . , .

+1

, ( ) .

, childclass ( ). - ().

, , , - childclass , ( , ).

, :

  • , . .
    , , .
  • (extends implements), , .
    , , , , ( ) , .
  • , .
    , , , , , , , , . - .
  • .
    , , . .
  • , ( , ) , , ( , , ).
+1

.

, . , , .

0

Source: https://habr.com/ru/post/1695690/


All Articles