0

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).

Machigan
  • 17
  • 7
  • What exactly isn't working in program #2? – tkausl Nov 15 '21 at 06:38
  • When I want to create a new Object with the constructor in the DeluxeBurger class (child class), the error says that the field has private access in Hamburger (parent class). – Machigan Nov 15 '21 at 06:41
  • 1
    Which field? Can you copy-paste the exact error message? – tkausl Nov 15 '21 at 06:42
  • Are this 2 classes on the same package? how about the file calling this? – crimson589 Nov 15 '21 at 06:43
  • "name has private access in Hamburger" and it's same for the field "meat", "breadRollTYpe", "price" – Machigan Nov 15 '21 at 06:44
  • Yes, both class is in the same project, I use the Main class to create new Object for this class @crimson589 – Machigan Nov 15 '21 at 06:46
  • 2
    `DeluxeBurger` doesn't access those fields, are you sure this is your code? – tkausl Nov 15 '21 at 06:49
  • Yes, this is my code. I haven't really understood the question @tkausl – Machigan Nov 15 '21 at 06:52
  • you can see that :[variable has private access](https://stackoverflow.com/questions/23463427/variable-has-private-access) – Rance Liu Nov 15 '21 at 07:05
  • With setter and getter, I have also the errors. And yes, I could use "protected" but I don't know why in the first program it's work without but not in the second – Machigan Nov 15 '21 at 07:11
  • What is the exact error message? The exact error message has at least two lines and should be something like first line: `DeluxeBurger.java:6: error: name has private access in Hamburger` and second line `System.out.println(this.name);`. Note that for that error message I had to tweak your code by adding the line `System.out.println(this.name);` to the `DeluxeBurger` constructor. – Thomas Kläger Nov 15 '21 at 07:38
  • @ThomasKläger Yes I have this error in a System.out.println() I don't write this part of code in my post to not overload the post. Sorry – Machigan Nov 15 '21 at 07:43
  • What sense does it make to post a question about an error if you remove the line that contains the error? Anyway, if DeluxeBurger needs the content of a field in Hamburger then either the field must be accessible in DeluxeBurger (which means making it protected) or the class Hamburger must provide a getter for that field (i.e. `public String getName()` and DeluxeBurger must then use that getter (i.e. `System.out.println(getName());` – Thomas Kläger Nov 15 '21 at 08:14
  • Yes, I am sorry for not post the System.out.println(), but that was not why I post that. i konw that we can use "protected" or getter but, I don't know why in my first program (that has also a System.out.println()) I don't have to use "protected" or getter but in the second I have to – Machigan Nov 15 '21 at 08:22
  • In the first example the child class Mitsubishi also doesn't have access to the parent classes name field. But since the examples are incomplete (the statements where the problem appears / doesn't appear are missing) it is hard to say what the difference is. – Thomas Kläger Nov 15 '21 at 09:53
  • Yes, I understand I will pay attention the next time enter all of the code. But strangely, Mitsubichi has access to the parent field – Machigan Nov 15 '21 at 10:02
  • No, it doesn't have access to the parent field. The Java compiler simply doesn't allow it. Maybe you changed the field from protected to private but then did not compile the `Car` class again (so that the subclass still compiles against the old version of the Car class) - things like these can happen if you don't use a build tool like Ant, Gradle or Maven (or forgot to delete the old class files after a change to the source files) – Thomas Kläger Nov 15 '21 at 10:28
  • Ho okay I see. So it's not normal that I have no error with the first program (with class Car) ? And for my futur program, I have to use "protected" field ? – Machigan Nov 15 '21 at 10:31

1 Answers1

0

I copy your code and past to my project and see your problem. i dont know Why, maybe its a bug. but if you create new package and drop this two class into new package Your problem will be solved and you will not see this problem again. It was interesting for me. thanx

hani
  • 49
  • 1
  • 7
  • If this did not work for you create new package and delete that classes and again create classes. it work for me – hani Nov 15 '21 at 07:21
  • I have tested by creating new package, but I have also this errors in the new package – Machigan Nov 15 '21 at 07:30
  • What I did exactly was move the class once to the correct package and then it was fixed. That's why I ask you to delete the whole class once and rebuild it in another package – hani Nov 15 '21 at 11:37
  • I use the refractor option vrom IntelliJ to move the class, but I have again the error – Machigan Nov 16 '21 at 09:23
  • maybe its a bug from Development Environment i use intellij idea and its work – hani Nov 20 '21 at 05:24
  • I use to IntelliJ – Machigan Nov 20 '21 at 10:00