+ 1
why it prints A?
5 Antworten
+ 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();
}
}
+ 2
A must be initiallizeed too, if you do not do it, compiler automatically add constructor A call without parameters
+ 2
ow i see
+ 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 .
0
You will need to make new B a different class instead of a child class if you want to print only new B.