public class Vegetable {

    private int calories;

    public Fruit (int calories){ 
        this.calories = calories; 

        }
}

public class Onion extends Vegetable {
    private String color;

    public Onion (int calories, String color) {
        super(calories);
        color = color; 
        }
}

Why doesn’t Onion constructor method properly set the color?

A. The Vegetable class’ calories attribute is private so the call to super doesn’t work.
B. The onion class’ color attribute is private, so it cannot be changed.
C. The constructor’s color variable hides Onion's color attribute so the wrong variable is changed.
D. The call to super doesn’t include the color.

Update: Please explain why you have chosen what you did?

I think the answer should be D but i'm confused?

The answer is Neither of those.

public class Vegetable {
    private int calories;
    public Fruit (int calories){ 
        this.calories = calories; 
        }
}

This won't compile. You can't have a class called Vegetable, and have a constructor (in said class) of type Fruit. This won't compile.
Let's assume you meant this:

public class Vegetable {
    private int calories;
    public Vegetable (int calories){ 
        this.calories = calories; 
        }
}

Answer A: this is not correct. super(calories); calls the existing constructor public Vegetable(int calories){} so this won't cause any trouble. If calories being private would cause problems, there wouldn't be a working application anywhere. You don't try to set the value outside of the Vegetable calss, so, no problem.

Answer B: this is not correct. Being declared doesn't dictate whether or not the value of a variable can be changed, rather to where it can be changed. You are working within the same class, so it has access to the private members.

Answer C: now here's the problem.

public Onion (int calories, String color) { // line 1
        super(calories);                        // line 2
        color = color;                          // line 3
        }

The color variable (both of them) mentioned in line 3, point to the variable declared in line 1, not the variable declared on class level.
To correct this, you must either inform the compiler you mean the instance variable:

public Onion (int calories, String color) {
        super(calories);
        this.color = color;   // this links to the instance you are working in
        }

or, you must make sure the input and instance variable have different names:

public Onion (int calories, String colorIn) {
        super(calories);
        color = colorIn; // now, there is no confusion on which variable you mean by color 
        }

Answer D: this is not correct. There is only one constructor in the super class, and it has only one parameter. Add the colors parameter to that super call, and your code won't compile, because it won't find the constructor you are calling. Since your super class (Vegetable) has no variable color, it makes no sense to pass color to it anyway.

Thanks for the correction. yes, it was suppose to be a vegetable and not fruit.

so the correct answer is C?

Yes.
color = color;
is completely using the local variable (which only exists within that constructor). Once that constructor's lifecycle ends ( at the } ) that variable is no longer available, and the value is not set to the instance variable. I've shown in my previous reply how to 'fix' the code, as well as explained why it fails.

Thanks, that makes alot of sense!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.