I have 2 differents programs that use class and class child. In both of the program I use private field. But in the first program that has for parent class "Car" I have no problem with private field, the children can has access to. But in the second program that has "Hamburger" for parent class, the children can not has acces to the fields :
First program (the program is working)
public class Car {
private boolean engine;
private int cylinders;
private String name;
private int wheels;
public Car(int cylinders, String name) {
this.cylinders = cylinders;
this.name = name;
this.wheels = 4;
this.engine = false;
}
}
public class Mitsubishi extends Car {
public Mitsubishi() {
super(5, "Mitsubishi");
}
}
Second program (not working)
public class Hamburger {
private String name;
private String meat;
private double price;
private String breadRollType;
private String addition1;
private String addition2;
private String addition3;
private String addition4;
public Hamburger(String name, String meat, double price, String breadRollType) {
if (price < 0) {
price = 0;
}
this.name = name;
this.meat = meat;
this.price = price;
this.breadRollType = breadRollType;
this.addition1 = "none";
this.addition2 = "none";
this.addition3 = "none";
this.addition4 = "none";
}
}
public class DeluxeBurger extends Hamburger{
public DeluxeBurger() {
super("Deluxe Burger", "Steak", 19.10, "Chic bread");
}
}
I have seen that I can use protected and it's fix the probelem, but I don't know why I have to use protected on the second program (with the Hamburger) but not on the first (with the Car).