Non-Default Constructors Are Not Inherited
Non-Default Constructors Are Not Inherited
class A {
public A() {
System.out.println("In A ctor");
}
}
class B extends A {
public B() {
System.out.println("In B ctor");
}
class A {
protected int x;
public A(int v) {
System.out.println("In A ctor");
x = v;
}
}
class B extends A {
private float z;
public static void main(String [] args) {
B obj;
obj = new B(3);
}
}
then you'd get a compiler error when you tried to
compile it. Why? Because when the compiler tries
to create an implicit constructor for B and cannot
find a corresponding constructor of A to call.
B obj;
obj = new B(3);
}
B(int i)
// because call "new B(3)" here, constructor is not inherited,
// must pass the appropriate parameters to match up with the version
// of the parental constructor you require., and "super(i)" is used to
// call the single integer version of the constructor in the base class.
{
super(i);
}
}
class Person {
String name = "No name";
//public Person(){};
public Person(String nm) {
name = nm;
}
}