0

I am having some troubles with my Java code. I need the fields and the parent class to be private YET they have to be useable in the subclass.

I need to run JUnite tests on the class and field modifier. Error code when running: Error PS: I tried making them protected but then you can change the fields.

Could use some advice. Thanks in advance

Parent class:

package logica;
import java.util.Objects;

public class Uitgave {
private String titel;
private double prijs;

private Uitgave(String titel, double prijs){
    this.titel = titel;
    this.prijs = prijs;
}

Subclass:

package logica;

import java.util.Objects;

public class Boek extends Uitgave{
private String auteur;
private int paginas;
public Boek(String titel,String auteur, double prijs, int paginas){
    super(titel,prijs);
    if(titel==null || auteur==null || prijs<0 || paginas<0){
        throw new IllegalArgumentException
("Controleer uw gegevens op fouten. 
Prijs en aantal paginas kan niet negatief zijn, 
het boek moet een titel en auteur hebben");
    }

    this.auteur = auteur;
    this.paginas = paginas;
}
JohnMcClane
  • 128
  • 6
  • Your missing a closing bracket in your constructor. – ryekayo Apr 21 '16 at 17:51
  • In my opinion, visibility of fields should always be private. You can add protected getter method in the parent class if you like. We still don't know what the issue is lol – Tin Apr 21 '16 at 17:53
  • and how would I be able to use that method in the subclass? just like: Parent class: private void getTitel(){return titel; Subclass: in constructormethod: this.titel=titel.getTitel(); or what? :) – JohnMcClane Apr 21 '16 at 17:54
  • I don't really see how you're having a problem with this. You simply replace 'private' with 'protected'. That's it. Also, don't do what Tin said, that will unnessarily clutter your code. – Lasse Meyer Apr 21 '16 at 17:54
  • That's like saying I want something painted red to look black. If you define them private, private they are (only visible in parent class). If you declare them protected, then you will be able to see them in the subclass. Other than that, you can add getters and setters if you want to keep them private. – Jose Cifuentes Apr 21 '16 at 17:54
  • 1
    Joachim, explain what the issue is with your program. We are all in the dark here, trying to guess what your question is. – RaminS Apr 21 '16 at 17:54
  • You have to use protected instead of private. Check following answer. http://stackoverflow.com/questions/215497/difference-among-public-default-protected-and-private – Madura Pradeep Apr 21 '16 at 17:55
  • Fields need to only useable by the subclass and the parent class cannot be modified, but for some reason I can't make it private – JohnMcClane Apr 21 '16 at 17:56
  • You can make private fields, but protected getters and setters which you can override in the subclass. – RaminS Apr 21 '16 at 17:59

1 Answers1

1

You have 2 options.

  1. Change private access modifier in to at least protected. But if you do this, the variable can access inside the package. Not only sub class (In Java, difference between default, public, protected, and private).

  2. Define get method in parent class for private variable and use super.getX() in sub class.

Community
  • 1
  • 1
Madura Pradeep
  • 2,378
  • 1
  • 30
  • 34