I keep getting an error saying that "call to super must be the first statement in the constructor".
The problem is that it is the first statement in my constructor.
public void CheckingAccountCustomer(int a){
super(n, p, b);
accountNo = a;
}
And here is my superclass for this as well.
public void customer(String n, int p, double b){
name = n;
pin = p;
balance = b;
}
What am I doing wrong here?
because it's a rule of the language, present in the Java Language Specification: a call to another constructor in the same class (the this(...) part) or to a constructor in the super class (using super(...) ) must go in the first line.
1) A super() or this() call must always be provided explicitly as the first statement in the body of a constructor. 2) If both a subclass and its superclass do not have any declared constructors, the implicit default constructor of the subclass will call super() when run.
It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor. The parameterized super() must always be the first statement in the body of the constructor of the subclass, otherwise, we get a compilation error.
It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
This code
public void customer(String n, int p, double b){
is not a constructor. Constructors don't have return types, e.g. void
. Assuming your class name is customer
:
public customer(String n, int p, double b){
This applies to CheckingAccountCustomer
too.
public void CheckingAccountCustomer(int a){
That's not a constructor since it states it has a void
return type. It's just a method of the same name as the class. Get rid of the return type.
public CheckingAccountCustomer(int a){
public void CheckingAccountCustomer(int a)
This is a method not a constructor, since it has a return type.
The constructor is used to create an instance of that Class, so it make no sense if it will let the user to change the return type (it can be dangerous too). That's why constructors has no return type.
As others have already answered, remove the return type and it'll become a constructor.
Constructors never return something (either void or Object type).
public void CheckingAccountCustomer(int a){
super(n, p, b);
accountNo = a;
}
thus is not a constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With