Class A:
public class A {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
Class B:
public class B extends A{
private int billNum;
B(String firstName, String billNum) {
super(firstName);
setBillNum(billNum);
}
public int getBillNumr() {
return billNum;
}
public void setBillNum(int billNum) {
this.billNum = billNum;
}
1.) Now i want to add a default like constructor like B() {}, but i am not allowed to do so. Why is this ?
In the subclass constructor you call a one-argument superclass constructor, which you didn’t declare. Add
A(String firstName) { this.firstName = firstName; }to the superclass. Alternatively, replace the linesuper(firstName);withsetFirstName(firstName);in the constructor of B.