Is Superclass Object created everytime a subclass is instantiated? [duplicate]


Is Superclass Object created everytime a subclass is instantiated? [duplicate]



This question already has an answer here:



This is a two part question :



PART #1 :


public class Test {

public static void main(String args) {

System.out.println("Main Started");
Child c = new Child(); //Instantiating Child Class
System.out.println("Main Ended");
}

}

class Father{

Father(){ //Father Class Default Constructor
System.out.println("I am in Father");
}

void show(){
System.out.println("Hello World");
}
}

class Child extends Father{

Child(){ //Child Class Default Constructor
System.out.println("I am in Child");
}
}



Output :


Main Started
I am in Father
I am in Child
Main Ended



This is what i know is happening at compile time


class Child extends Father{

Child(){ //Child Class Default Constructor
super();
System.out.println("I am in Child");
}
}



here my question is, since it is going to the default constructor of the Father Class before executing the default constructor of Child Class, is there an object created for Father?


Father


Child


Father



if so, then what is it getting assigned to? or is it similar to anonymous object like new Father();. Is an Object created EVERYTIME a constructor is called?


new Father();


Object



PART #2



Every single time i create an object for the Child Class, the default constructor of the Father Class will be called using super(); within Child().


Child


Father


super();


Child()



What will happen when I am trying to invoke any method from Father using the Child object? will it again create an anonymous object for Father, use the object for execution then leave it for Garbage Collector to take care of?


Father


Child


Father


Garbage Collector



Because of so, wouldn't this lead to tons of memory wastage simply because we tried to access the members of supper class multiple times.



How will the memory management take place in the above mentioned scenario?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





This misconception is often caused by fact that people think that constructor creates object. It is not true, it only initialize (set up) object which was already created by new operator (yes, new keyword creates object first, then after it was created is calling constructor's code to properly set it up).
– Pshemo
Jun 30 at 6:50


new


new





@Pshemo so what does this SET UP for the object mean exactly.
– 404 Brain Not Found
Jun 30 at 6:54





Before we let object be used, we want to make sure that it was properly set up. By that I mean ensuring that its fields hold values which "make sense" for application. For instance object of File class should always hold some info about location of file (or directory). Allowing us to use instance of File without such information (if that field holds null) would be designers mistake so Java has File(String path){..} constructor via which we can pass location (correct or even incorrect, as long as it "makes sense" for us/application). Only then File instance can be used.
– Pshemo
Jun 30 at 7:39



File


null


File(String path){..}




1 Answer
1



When you create a Child instance you aren't creating an additional Father instance. The Child instance is also a Father instance, since Child extends Father. Child is a specific type of Father.


Child


Father


Child


Father


Child


Father


Child


Father



One neat way of showing this is to print System.identityHashCode in both classes, and seeing that both the Father and the Chilld constructors relate to the same memory address:


System.identityHashCode


Father


Chilld


public class Test {
public static void main(String args) {

System.out.println("Main Started");
Child c = new Child(); //Instantiating Child Class
System.out.println("Main Ended");
}

}

class Father{

Father(){ //Father Class Default Constructor
System.out.println("I am in Father in address " + System.identityHashCode(this));
}

void show(){
System.out.println("Hello World");
}
}

class Child extends Father{

Child(){ //Child Class Default Constructor
System.out.println("I am in Child in address " + System.identityHashCode(this));
}
}



And an example output:


Main Started
I am in Father in address 2001049719
I am in Child in address 2001049719
Main Ended





Perfect, exactly what i was looking for. Thank you for the example. Just one more thing, and this is in context to the example you have given above. So, in inheritance, even though the Super Class constructor is called, but its object is not created. Is this Accurate?
– 404 Brain Not Found
Jun 30 at 6:51


Super Class





@404BrainNotFound There is only one object, it is of Child class, but at the same time of Father type and that object already exist when constructors code is executed (which is why we can use this inside constructors to refer to such object).
– Pshemo
Jun 30 at 6:54



Child


Father


this