Reputation: 133
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.
Upvotes: 6
Views: 6666
Reputation: 1
It will load the class of parent, but not create the instance. You can wirte a simple code and use Java VisualVM to see the objects snapshot.
Upvotes: 0
Reputation: 131346
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 :
Object
) until the closest parent class(B
).Object
) until the closest parent constructor(B
).Upvotes: 4
Reputation: 1733
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:
extends
or implements
) it is guaranteed that the child will also automatically provide the same methods. Upvotes: 2
Reputation: 10717
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.
Upvotes: 2
Reputation: 311228
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.
Upvotes: 0