I have two abstract classes i.e. Medicine and Prescription. All code can be found at https://codeshare.io/aVAdr3 These two classes have subclasses, the class hierarchy diagram is as follows:
and…
The medicine java file:
abstract class Medicine { public String name; public int price; public Medicine (String name, int price) { this.name = name; this.price = price; } public int getPrice () { return price; } public void setPrice (int newPrice){ price = newPrice; } } class commonDrug extends Medicine { public commonDrug (String name, int price) { super(name, price); } }
The Prescription java file:
abstract class Prescription { protected Medicine med; public Prescription(Medicine med) { this.med = med; } } class bluePrescription extends Prescription { public bluePrescription (Medicine med) { super(med); System.out.println(med.getPrice()+ "<-- Price for bluePrescription, it should be 30, but the calculations in pPrescriptions affect it."); } } class whitePrescription extends Prescription { public whitePrescription (Medicine med) { super(med); } } class pPrescription extends whitePrescription { public pPrescription (Medicine med) { super(med); System.out.println(med.getPrice()+ "<-- Price before calculation for pPrescription"); //Calculations int priceWithDiscount; if (med.getPrice()<=20) {priceWithDiscount=0;} else {priceWithDiscount= med.getPrice()-20;} med.setPrice(priceWithDiscount); System.out.println(med.getPrice()+ "<-- Price after calculation for pPrescription"); } }
The test program is as follows:
class TestProgram { public static void main (String[] args) { //Medicine object commonDrug drug1 = new commonDrug("Paracetamol", 30); //Prescription objects: pPrescription prescription1 = new pPrescription(drug1); bluePrescription prescription2 = new bluePrescription(drug1); } }
And when you run the test program you get this in the terminal:
30<-- Price before calculation for pPrescription 10<-- Price after calculation for pPrescription 10<-- Price for bluePrescription, it should be 30, but the calculations in pPrescriptions affect it.
I’ve been trying to solve this for hours, I can’t figure out how I can perform calculations in the pPrescription constructor without affecting instances of bluePrescription. Why is this happening? pPrescription is a subclass of whitePrescriptions, not bluePrescriptions. Anyhow, to instances of a class are completely separate, getPrice and setPrice are not static, so why is using them affecting all the instances of Medicine?
Advertisement
Answer
why is using them affecting all the instances of Medicine?
There is only once instance of Medicine
in your code.
You pass the same object, i.e. drug1
to both pPrescription
and bluePrescription
class constructors.
As there’s only one object (drug1
) that is passed to both classes, if any class modifies it, changes will be reflected everywhere you refer to that object.
One way to fix the problem is to not save the discounted price and just calculate it whenever you need it using a method in the pPrescription
class.
class pPrescription extends whitePrescription { ... public int getDiscountedPrice() { return med.getPrice() <= 20 ? 0 : med.getPrice() - 20; } }
Side note: Class names should begin with a capital letter.