I am having trouble running this program that tests for inheritance in java. I made two classes, one Drink class that is supposed to be the parent class, and one tea class that is supposed to be the child class. I made a constructor that calls to the parent constructor with super() in order to create a new tea object. However, the compiler is telling me that the field for one of the parameters I am sending in (caloriesPerOunce) is not visible. Even though I didn't require the parameter to be included in the constructor for Tea(), I did declare and instantiate it later in the block. Is this an issue with my code or is there something in the settings I should change since I am using an eclipse IDE?
public class Drink {
private String name;
private double quantity;
private int caloriesPerOunce;
public Drink(String name, double quantity, int caloriesPerOunce)
{
this.name = name;
this.quantity = quantity;
this.caloriesPerOunce = caloriesPerOunce;
}
public class Tea extends Drink{
private boolean isSweet;
public Tea(String name, double quantity, boolean isSweet)
{
super(name, quantity, caloriesPerOunce); //here it tells me that the field Drink.caloriesPerOunce is not visble
int caloriesPerOunce = 100;
this.isSweet = isSweet;
}
CodePudding user response:
The tea class constructor has no argument named caloriesPerOunce
. Are you trying to access the variable from the Drink
parent class then that wont work since the scope of caloriesPerOunce
is private. The caloriesPerOunce
variable could be accessed if the classes are nested then why use inheritance?
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
CodePudding user response:
Java is not JavaScript
Short answer: Stop pretending that Java is JavaScript.
Longer answer:
In this code:
super(name, quantity, caloriesPerOunce);
int caloriesPerOunce = 100;
You are 100% attempting to access the parent caloriesPerOunce
variable.
This is because you do not declare the local caloriesPerOunce
until after the super
"call".
In Java (unlike JavaScript) there is no hoisting of variables to the top of scope.
If you want to pass 100 to the parent constructor, then pass 100 to the parent constructor.
super(name, quantity, 100);
If 100 is a constant value for Tea, then you can declare a "constant" field for the class and pass the "constant" field. For example,
private static final int TEA_CALORIES = 100;
public Tea(String name, double quantity, boolean isSweet)
{
super(name, quality, TEA_CALORIES);
... other stuff.
}
CodePudding user response:
Since the caloriesPerOunce
Field is declared as 'Private', no other class than itself can access that.
Change the line:
private int caloriesPerOunce;
to this:
public int caloriesPerOunce;
Also, if you are using this arg in super()
call, you have to first declare the variable before it, so change your Tea class this way:
public class Tea extends Drink {
private boolean isSweet;
public Tea(String name, double quantity, boolean isSweet, int caloriesPerOunce) {
super(name, quantity, caloriesPerOunce);
this.isSweet = isSweet;
}
}
And you can use it this way:
Drink drink = new Tea("Chocolate", 7, true, 50);