13

I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using

public crisps(String flavour, int quantity) {
    this.flavour = super.getFlavour();
    this.quantity = quantity;
}

In the superclass I have initialised the field with

private String flavour;

and I have an accessor method

public String getFlavour() {
    return flavour;
}

I am getting an error "flavour has private access in the superclass", but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user215732
  • 153
  • 1
  • 3
  • 10

4 Answers4

22

What you should do:

Add a constructor to your super class:

public Superclass {
    public SuperClass(String flavour) {
       // super class constructor
       this.flavour = flavour;
    }
}

In the Crisps class:

public Crisps(String flavour, int quantity) {
    super(flavour); // send flavour to the super class constructor
    this.quantity = quantity;
}

 

Comments

Some comments to your question:

"In the superclass I have initialised the field with "

private String flavour;

This is not an initialization, it is a declaration. An initialization is when you set a value.

"I am getting an error " flavour has private access in the superclass" but I believe this shouldn't matter as I am calling the accessor method that returns it to the field?"

When you call a accessor (aka getter), it is ok - depends on the getter visibility. The problem in you code is the:

this.flavour = 

because flavour is not a field declared on Crisps class, but on the supper class, so you can't do a direct access like that. you should use my suggestion or declare a setter on the super class:

public void setFlavour(String flavour) {
    this.flavour = flavour;
}

Then you can use it on the child class:

public Crisps(String flavour, int quantity) {
    this.quantity = quantity;
    super.setFlavour(flavour);
}
lpinto.eu
  • 2,077
  • 4
  • 21
  • 45
  • 1
    @ipinto.eu while your answer is correct and so is your solution, your last alternative solution in your answer (using setters of superclass) violates the rule of inheritance, I suppose, as it would give the "[Overridable method call in constructor](https://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors)" problem. Hence, you last alternative solution is not an actual solution, but would bring bugs within the code. I think, *super.setFlavour(flavour)* should work. – Sajib Acharya Jul 27 '17 at 13:54
  • I understand and agree; I must say, the example was for the sake of the discussing. I like your suggestion of calling it using super, Other options (and different philosophy) is to make your setters final (not possible to override). – lpinto.eu Jul 27 '17 at 18:00
2

flavour is private. Although you're reading it from the public method, you're assigning it to a private field, and you likely didn't declare it in this class.

You could set flavour to protected in the parent class or define a setter for it

Ultimately your code doesn't really make sense though. Even if it did compile, it would be more or less: flavour = flavour. Perhaps you should rethink what you're trying to do a little bit

I think you may need a tighter grasp on Java and Object Oriented Programming.

http://docs.oracle.com/javase/tutorial/java/concepts/

You should start here.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • ok, is there anyway to do it by calls to the public method getFlavour? – user215732 Oct 11 '13 at 20:06
  • @user215732 `getFlavour` does just that. It gets `flavour`. You cannot use this to assign a value to flavour. If it returned a mutable object then you could modify the actual flavour, but in this case strings are immutable. – Cruncher Oct 11 '13 at 20:08
  • 1
    @user215732 Your `crisps` constructor has a parameter `flavour` but you are doing nothing with it, as your code is currently written. – ajb Oct 11 '13 at 20:09
  • @user215732 Create a `public` setter. – Sotirios Delimanolis Oct 11 '13 at 20:10
  • @SotiriosDelimanolis It'll sure look funny after he adds a setter. `super.setFlavour(super.getFlavour());` Granted, it looks pretty funny now. – Cruncher Oct 11 '13 at 20:12
  • @Cruncher I think OP wanted to set the value, not get the value. Also, OP, look into `super` constructors. – Sotirios Delimanolis Oct 11 '13 at 20:13
  • @user215732 That's why I edited my answer with a link to where you can learn the concepts – Cruncher Oct 11 '13 at 20:14
  • @SotiriosDelimanolis I agree, that's likely what he meant. I'd like to add that, until you need to set this variable from other classes, it would be better to make the setter protected, instead of public. The super constructor is probably better still. – Cruncher Oct 11 '13 at 20:15
2
public crisps(String flavour, int quantity)
{
    super(flavour);
    this.quantity = quantity;
}

This should work as see Docs

HansB
  • 21
  • 1
2

make

    private String flavour;

public,otherwise your subclasses won't have access to this String. Your superclass doesn't know about existence of any subclass. According to Java documentation, "private" makes any variable and method available within that class,where private variable or method was declared, no any class has access to it,even subclasses. Once you chance your access modifier, you won't get any errors.

AI I
  • 331
  • 5
  • 15