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