Reputation: 23
I have got some methods that are common to two subclasses so i have put them in Abstract superclass but there is one method that uses a variable with two different values so i am confused how to implement that method the code is like:
StandardMember class got this method with its own different value for remainingCredit=30 at starting
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
// TODO Auto-generated method stub
System.out.println("Hello");
Holding tempHolding = Library.libCollection.getHolding(holdingId);
if (Library.libCollection.getHolding(holdingId) != null) {
if (tempHolding.isOnLoan()) {
System.out.println("Can not be issued Currently on Load");
} else {
System.out.println("Can be issued");
remainingCredit-=tempHolding.getDefaultLoanFee();
System.out.println(getRemainingCredit());
tempHolding.setLoanCheck(true);
currentlyBorrowedHolding.put(holdingId, tempHolding);
System.out.println(remainingCredit);
System.out.println(holdingId);
}
}
PremiumMember Class got same method but the value of remainingCredit is 45, Whereas all the methods common to them are implemented in this AbstractMember class which implements the Member interface. but when i try to call these method from other class i have to initialize the object of AbstractMember in Library Class like this
Member member = new StandardMember();
which is very bad because I can not use the StandardMember object to run the PremiumMember Object's version of same method. so, either i should create new object of PremiumMember Class or i dont know what to do. but if i create two objects then this member object is being used in borrowHolding method which is basically a cascading method in Library Class which in turn calls the borrowHolding method in Member Interface:
public void borrowHolding(int holdingId) throws InsufficientCreditException, MultipleBorrowingException {
if(libCollection.holdingMap==null){
System.out.println("Collection is Empty");
}
if(libCollection.holdingMap.containsKey(holdingId))
member.borrowHolding(holdingId);
}
the problem is i can not create two objects because at runtime i can only call one method. so help me out how to implement this method in Abstract class so that program will detect the difference that which object it should create.
Upvotes: 0
Views: 538
Reputation: 7383
If I understood you correctly, you have a Member
abstract class with a field remainingCredit
which has to be 30
in one subclass and 45
in another.
Use a protected constructor.
public abstract class Member {
private int remainingCredit;
// Other members
// getters and setters
protected Member(String memId, String memName, int remainingCredit) {
this.memId = memId;
this.memName = memName;
this.remainingCredit = remainingCredit;
}
}
public StandardMember extends Member {
public StandardMember(String memId, String memName) {
super(memId, memName, 30);
}
}
public PremiumMember extends Member {
public PremiumMember(String memId, String memName) {
super(memId, memName, 45);
}
}
Upvotes: 1