0
Why another constructor is not running during class B initialization?
class A { public A() { System.out.println("New A"); } public A (String name){ System.out.println("New A 1"); } } class B extends A{ public B() { System.out.println("New B"); } } class Program { public static void main(String[ ] args) { B obj = new B(); } } //output New A New B but not also New A 1
5 Antworten
+ 4
Because only the default constructor is called. You have not initialized an object with the constructor that takes a String parameter.
+ 1
you have to pass a string as an argument/parameter while you create an object/instance of the class..
0
you have to call the A constructor with string in order to have New A 1 example A obj = new A ("dfgg") or B obj = new B ("sdfdd")
0
for that u need to pass string as a parameter there.
like
B obj = new B("hello");
0
In class B In the no argument constructor the compiler is going to place
public B()
{
super();
sop("New B");
}
it means it's a call to no argument constructor for the parent class. if the parent class doesn't have no argument constructor then it call's default constructor of parent class.