In my program I have 4 classes like the following:
class Bankclass Branch extends Bankclass Customer extends Branchclass Loan extends Customer
If I want to add a Customer, then I give all the parameters from main function. like:
customer(bank name, bank add, branch number, brunch add, customer info...)
From this constructor, bank’s information goes to the Bank class via both Customer and Branch constructors using super() and so on.
The problem is when I created the Loan, class extending the Customer class, then I didn’t use any constructor, and I don’t want to use it too, because I want only one variable from the Customer class. But my compiler showing this error, it is saying to use a constructor for taking all the parameters for Bank, Branch, and Customer‘s constructors.
How can I solve this problem without using any constructor in the Loan class?
The simplest way to avoid using the
Customerconstructor in theLoanclass is to not extendCustomer. When deciding what class to extend, if any, you should use the “is a” test. For example, aBranchis aBank, so it makes sense to haveclass Branch extends Bank. However,Loanis aCustomerdoesn’t really make sense, so you shouldn’t doclass Loan extends Customer. On the other hand, aCustomerhas aLoan; in fact, aCustomermay have more than oneLoanor none at all. This means that it makes sense to have aLoanmember variable (or perhaps aListofLoans, if you have learned about the Container API) in theCustomerclass.