简体   繁体   中英

will there be object creation of parent class if we create an object of child class?

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

Thanks in advance.

You have a single object.
As a class is instantiated, it invokes first the constructor of the parent class. And for each parent class, it follows the same logic : it invokes first the constructor of the parent class.

Suppose : A extends B extends C .

new A() results to the B constructor invocation that invokes itself the C constructor.
As C then B constructor invocations are returned, the A constructor can go on and the instance A is created.

But, what I specifically want to know is that will it load the class or will it create the object of that parent class.

Similarly to the constructor execution, before loading a class, its parent class has to be loaded first and that recursively for each parent class.

Here classes corresponding to 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();
    }
}

Compile these classes and then execute the Test class that creates a A instance in its main() method by specifying the -verbose flag (enable by default the classes logs) in the java command :

java -verbose Test

And you should see in the output standard something like :

[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

It confirms that :

  • the parent classes need to be loaded first by starting by the root parent class ( Object ) until the closest parent class( B ).
  • no constructor is executed before all classes of the hierarchy be loaded
  • the constructors of the parent classes are always executed before the constructor of the instantiated class by starting by the root parent constructor ( Object ) until the closest parent constructor( B ).

The constructor is called, therefore an object of the parent class is created. That object is used later to build an object of the child class. The reason for this is that an object of the child class is an object of the parent class with more things.

Because the child class inherits properties and methods from the parent class, the entire class hierarchy (including superclasses and implemented interfaces) are loaded .

When you create an instance of the childclass, only a single object of type childclass is created (instantiated). There are no additional instances of any parent(s).

The properties and methods declared on parent classes are accessible via the childclass instance, and due to this inheritance the childclass instance will be accepted as argument to any methods that expect an instance of the parent (or any interfaces that it or any parent implements).

If you think of the code as a contractual design , this makes perfect sense:

  • The parent class declaration establishes that certain methods are available to call on itself or any of its children. The same is true for interfaces.
    For example , all employment contracts in the real world must contain certain clauses, but the rest is up to the company.
  • By inheriting ( extends or implements ) it is guaranteed that the child will also automatically provide the same methods.
    For example , we can assume that any law-abiding company will provide employment contracts including those clauses and (once agreed) the signature of both you and the company.
  • The entire hierarchy needs to be loaded in order to understand the full contract.
    For example , if there is a dispute, the lawyers who examine a contract will check what the law prescribes, perhaps by looking at a sample contract provided by the legislating body. The parentClass is that sample in the problem.
  • We only need one object instance.
    For example you won't get two employment contracts, one for the basic stuff and another with more information. You will receive and sign a single document.
  • Finally, in my analog, any employment contract (no matter how it is phrased) will be acceptable to any agency that asks you to prove your relationship to the company that employs you (assuming it is legal and valid by containing the legally prescribed clauses and appropriate signatures).

When you first load a class, all its ancestors are also loaded recursively.

When you instantiate an object you won't instantiate an additional instance of its parent. However, the object you just instantiated is itself also an instance of the parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM