-1

I have a parent class in which i have a final static int which i need to shadow in a child class. In the parent class i use a method "print", which i override in my child class. If i use this code, the method print will use the MINIMUMPRIJS from my parent class. How can i make it use the MINIMUMPRIJS from my childclass?

In my taskdescription it says about the class "reis": - Make sure the price is at least 5 euros. Create a variable for this and make sure this minimumprice is always guaranteed.
- 1 constructor with 1 paramter (destination)
- 1 constructor with paramters (destiantion, price)

about the class "vliegtuigreis"
- In here the minimumprice is 25 euros. Do what is needed.
- 1 constructor with just 1 paramater (destination)
- 1 constructor with paramaters (destination, price, and flightnumber)

public abstract class Reis {
    public final static int MINIMUMPRIJS = 5;

public void setPrijs(double prijs) {
        if (prijs >= getMINIMUMPRIJS()) {
            this.prijs = prijs;
        }else{
            this.prijs = MINIMUMPRIJS;
        }
    }

    public void print(){
        System.out.printf("Reis met bestemming %s kost %,.2f euro. %n", bestemming, prijs);
    }
}

public class VliegtuigReis extends Reis {
    private final static int MINIMUMPRIJS = 25;

    public void print(){
        super.print();
        System.out.println("Vluchtnr " + vluchtnummer);
    }
}
wetallday
  • 31
  • 1
  • 7
  • 3
    You can't shadow it, that's what `static final` means. You've got to give it a different name. – Andy Turner Aug 18 '16 at 15:15
  • So i need to copy my code from the parent "print" and add it to the "print" from my child class aswell? – wetallday Aug 18 '16 at 15:17
  • 2
    Well, you're never actually referring to `MINIMUMPRIJS` in your code... – Andy Turner Aug 18 '16 at 15:18
  • Well the "prijs" in the first "print" method becomes the "MINIMUMPRIJS" if the input for that variable is wrong. This happens in another method. `public void setPrijs(double prijs) { if (prijs >= getMINIMUMPRIJS()) { this.prijs = prijs; }else{ this.prijs = MINIMUMPRIJS; } }` – wetallday Aug 18 '16 at 15:21
  • What's `getMINIMUMPRIJS()`? Also: `this.prijs = Math.max(MINIMUMPRIJS, prijs);`. – Andy Turner Aug 18 '16 at 15:25
  • My getMINIMUMPRIJS returns 25 – wetallday Aug 18 '16 at 15:27
  • Please include a short, self-contained example which demonstrates your issue, so that we don't have to ask what you mean for symbols and how you are using them. – Andy Turner Aug 18 '16 at 15:29
  • The problem is that my Method "print" in my parent class needs to use the `MINIMUMPRIJS` from my child class, while there exists a `MINIMUMPRIJS` in my parent class. But the method uses the `MINIMUMPRIJS` from my parent class instead. – wetallday Aug 18 '16 at 15:43
  • If you have a method `getMinimpmuPrijs` (That's the appropriate capitalization according to the Java language conventions), then in the parent, you return the value of the parent constant from it, and in the child, you return the value of the child constant from it. Simply don't use the constant directly except inside that method. – RealSkeptic Aug 22 '16 at 06:45

1 Answers1

0

You can define constants for default values, but then use fields to store dynamic values. You don't need to "shadow" the minimum price of the parent at all, you merely refer to a different constant in the child.

public abstract class Parent {
    public static final double MIN_PRICE = 5.0;
    private double price;

    public Parent() {
        this(MIN_PRICE);
    }

    public Parent(double price) {
        this.price = Math.max(price, Parent.MIN_PRICE);
    }

    public print() {
        System.out.println("Price is:  " + this.price);
    }
}

public class Child extends Parent {
    private static final double MIN_PRICE = 25.0;

    public Child() {
        super(Child.MIN_PRICE);
    }

    public Child(double price) {
        super(Math.max(price, Child.MIN_PRICE));
    }

    public void print() {
        super.print();
        System.out.println("Other stuff");
    }
}

Hopefully you can take this and expand it to the rest of your requirements.

Zach Olivare
  • 3,805
  • 3
  • 32
  • 45