For example I have this:
class A{
private int mine = 0; //Some field...
public A(int a){mine+=a; /*Some operation1...*/}
}
class B extends A{
private int mine = 0; //Some field...
public B(int a){mine-=a; /*Some operation2...*/}
}
and I get:
error: constructor A in class A cannot be applied to given types;
public B(int a){}
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 errors
I don’t understand the error? What is telling me to do?
Though, the code works if Constructor of “A” has no arguments.
But I need to do the Operation1 (aka mine+=a;), so I need A’s arguments, but then I fail.
I’m closed in this magic circle. What shall I do?
The first instruction of every constructor is always to invoke one of its superclass constructor. If you don’t do it explicitely, the compiler inserts this instraction for you. The constructor
is thus equivalent to
Since A doesn’t have a no-arg constructor, the compilation fails. In this case, you must explicitely invoke one of the super constructors: