Ali Hayder
Ali Hayder

Reputation: 361

Don't want to use constructor in subclass

In my program I have 4 classes like the following:

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?

Upvotes: 1

Views: 267

Answers (3)

Frank
Frank

Reputation: 15631

You will have to revise your OO thinking:

Branch extends Bank, Ok i can live with it, but possible not a good idea (Maybe a decorator pattern is a better way to go)

Customer extends Branch, this is not correct (try to think real live...) a Branch will have a collection of customer and possible a Customer has a field Branch, depending on your needs.

Loan extends Customer, this is not correct, a like above a Customer can have a collection of Loans and possible a Loan has a collection of Customers, depending on your needs.

So I advise you to review your object model.

Please note that having 2 way references is only a good idea if you really need it, but as i don't know your requirements i can't decide that for you.

Some articles to read:

Decorator pattern

Composition over inheritance

Upvotes: 0

jalynn2
jalynn2

Reputation: 6457

Don't use inheritance, use references for these classes:

class Bank {
    private List<Loan> loans;
}

class Loan {
    private Customer customer;
}

You can model this different ways: A Bank has a list of Customers, and a List of Loans, A Loan has a Customer or a Customer has a list of Loans, etc. But the important point is that one class should only extend another when it "is" that type: a Dog may extend Animal, but a Dog would not extend Kennel or Food.

When you do this you can create a Bank, passing its attributes to it in the constructor. Then create a Customer, passing only the Customer attributes to its constructor. To associate the customer to the bank, add this method to Bank:

public void addLoan(Loan loan) {
    this.loans.add(loan);
}

Then each class's constructor only needs its own attributes.

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

The simplest way to avoid using the Customer constructor in the Loan class is to not extend Customer. When deciding what class to extend, if any, you should use the "is a" test. For example, a Branch is a Bank, so it makes sense to have class Branch extends Bank. However, Loan is a Customer doesn't really make sense, so you shouldn't do class Loan extends Customer. On the other hand, a Customer has a Loan; in fact, a Customer may have more than one Loan or none at all. This means that it makes sense to have a Loan member variable (or perhaps a List of Loans, if you have learned about the Container API) in the Customer class.

Upvotes: 1

Related Questions