-4

I'm creating some Java code of an array of Flying Objects, which all have an attribute price (double). I am somewhat new to inheritance and this is is my first time using the super keyword. When I create a child class object Airplane into the array, the price feature does not seem to properly pass through the constructor.

Here are my FlyingObject constructors:

public class FlyingObject {

protected double price;

public FlyingObject()
{
    price = 0;
}

public FlyingObject(double aPrice)
{
    price = aPrice;
}

and here are my Airplane constructors:

// CONSTRUCTORS

public Airplane ()
{
    super();
    brand = "Unknown brand";
    horsepower = 0;
}

public Airplane (String aBrand, double aPrice, int aHorsepower)
{
    super(aPrice);
    brand = aBrand;
    horsepower = aHorsepower;
}

Everything when I create the airplane with arguments String, double and int, the string (brand) and int (horsepower) get registered correctly, but the price stays at 0. Is there something I am doing obviously wrong that I am missing? Any help would be greatly appreciated.

EDIT: Found what I did wrong.Silly mistake.

In my Airplane class, I had redefined price as an instance variable and had forgot about it, which overwrote the price from FlyingObjects.

As soon as I took out that extra price variable and only had the price variable come from the superclass (as intended) then everything worked fine.

Will post better examples (reproducible) next time. Cheers

halien51
  • 1
  • 1
  • Does this answer your question? https://stackoverflow.com/questions/51845169/java-initialize-base-class-fields-in-subclass-constructor – SnowmanXL Oct 19 '20 at 06:52
  • Unrelated: dont have two constructors doing different things. Have your no-arg constructor in FlyingObect invoke the *other* constructor, like `this(0)` instead! – GhostCat Oct 19 '20 at 06:53
  • 5
    Beyond that, please provide a real [mcve]. The code you are showing looks correct. So please give us the code that shows the problem. You see, you make assumptions about your code (gets registered correctly), but if your assumptions about your code were all correct, you wouldnt be here asking for help. So, please: edit your question and add more code. – GhostCat Oct 19 '20 at 06:54
  • Hi all, apologies for not being familiar yet with the best practice on asking questions on here. I'll make sure to include a minimal reproducible example in the future. Thanks to all for your answers. I will be looking at the code later today to see if I can make the code reappear in this small reproducible example. Feel free to close this question and if the problem persists I can repost it, albeit with the correct example. – halien51 Oct 19 '20 at 15:11

1 Answers1

1

I'm not really sure what the issue you're having is. I just tried it out in jshell, and it seems to work pretty well:

public class FlyingObject {
    protected double price;

    public FlyingObject() { price = 0; }

    public FlyingObject(double aPrice) { price = aPrice; }
}

public class Airplane extends FlyingObject {
    int horsepower;
    String brand;

    public Airplane() { 
        super(); 
        brand = "Unknown brand";
        horsepower = 0;
   }

   public Airplane(String aBrand, double aPrice, int aHorspower) {
        super(aPrice);
        brand = aBrand;
        horsepower = aHorsepower;
   }
}

If I then call Airplane plane = new Airplane("Boeing", 500000, 800); I get an Airplane object, the results of which are that plane.price = 500000.

Also just as an aside, you might want to consider (instead of aHorsepower, aBrand, etc.) just using horsepower and brand in your constructors, and then using the this keyword as follows:

public Airplane(String brand, double price, int horsepower) {
    super(price);
    this.brand = brand;
    this.horsepower = horsepower;
}
  • 2
    I appreciate the effort you took to help the questioner, but well: "unable to repro the problem" is a clear indication that this question should be closed, not answered. I already wrote a comment exactly about that. So, from that point of view: please learn a bit how/what people are supposed to ask here. You see, this question will be closed unless the OP improves it. And then probably deleted, and your nice answer with it. So: better spent your time and energy on questions that **can** be answered. Telling the OP "unable to repro" isnt an answer. – GhostCat Oct 19 '20 at 07:14
  • Oh, okay. Thanks for the advice! – Ben Rosenberg Oct 19 '20 at 07:53
  • Ben Rosenberg, thank you very much for your answer. As I noted above, it seems my post does not abide by the guidelines of providing a reproducible example, so I have told the mods they may close it if they wish, and I will look at my code and try to reproduce the example. If the problem persists, I will make a new (more correctly formulated) post. But I want to thank you for looking at it. Perhaps my problem comes from the implementation of the class via my driver class, though I am not sure why yet. Thank you in any case for looking into it, much appreciated. – halien51 Oct 19 '20 at 15:12