does the base class constructor automatically called whenever an object is created for subclass? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

does the base class constructor automatically called whenever an object is created for subclass?

23rd Aug 2016, 2:36 PM
Raghul Kumar
Raghul Kumar - avatar
2 Answers
+ 1
yes. whenever an object created in child class the constructor with no arguments in that class will be executed. as a part of child class constructor in the very first line compiler places super() means it's a call for parent class no argument constructor if parent doesn't have any no argument constructor then it calls default constructor of the parent class. class Parent { Parent() { super();// compiler will place it automatically. sop("parent class no argument constructor"); } } class Child extends Parent { Child() { super();// compiler will place it automatically. sop(" child class no argument constructor"); } P S V M(String...args) { new Child(); } } OUTPUT: parent class no argument constructor child class no argument constructor conclusion: Though we are not creating object for parent class, because of that super(); which is placed by the compiler in every constructor control goes from child class no argument constructor to parent class no argument constructor and executing statements present in parent class no argument constructor, followed by child class no argument constructor statements execution.
24th Aug 2016, 2:57 AM
Sarath chalapaka
Sarath chalapaka - avatar
0
tq Satath😀
25th Aug 2016, 8:51 AM
Raghul Kumar
Raghul Kumar - avatar