why it prints A? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
15th Oct 2019, 12:46 AM
Sanzid Sadman
Sanzid Sadman - avatar
5 Answers
+ 7
When you will create a object child class then the parent class constructor will be invoke class A { public A() { System.out.println("New A"); } } class B extends A { public B() { // At the time of compling // Complier will call the super class constructor // while creating a child class constructor super(); System.out.println("New B"); } } class Program { public static void main(String[ ] args) { B obj = new B(); } }
15th Oct 2019, 2:47 AM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 2
A must be initiallizeed too, if you do not do it, compiler automatically add constructor A call without parameters
15th Oct 2019, 2:26 AM
zemiak
+ 2
ow i see
15th Oct 2019, 3:30 PM
Sanzid Sadman
Sanzid Sadman - avatar
+ 1
Your program prints New A New B Why? Because in main function you are declaring object of class B which is child class of class A Complier see that you create object of class b but class b is extended to class A instead of going inside of class B it goes to it's parent class because it see class B extends class A {} Then it's prints new A from class A And then prints new B from class B after compiling class A .
15th Oct 2019, 1:33 AM
Chirag Kumar
Chirag Kumar - avatar
0
You will need to make new B a different class instead of a child class if you want to print only new B.
16th Oct 2019, 9:21 AM
Fernando Pozzetti
Fernando Pozzetti - avatar