0

im wondering why only can set the data in the constructor if i define in in that way:

    public Auto(int ps, String kennzeichen) {
       super(ps,kennzeichen);
       this.ps = ps;
       this.kennzeichen = kennzeichen;
    }

If i leave this.ps and this.kennzeichen it will be initialized with null. I thought 'super' defines to inherit the data from my Vehikel-class-constructor.

Main:

public class Main {

public static void main(String[] args) {

    ArrayList vehikel = new ArrayList();
    Scanner scinner = new Scanner(System.in);
    boolean eingabeFortf = true;

    Auto auto = new Auto(12, "HE-ML 123");

    System.out.println(auto.getKW());
    System.out.println(auto.getKennzeichen());
    System.out.println(auto.getPS());

Vehikel:

public abstract class Vehikel {

private int ps;
private String kennzeichen;
public final double uFaktor = 0.735;



public Vehikel(int ps, String kennzeichen) {
    this.ps = ps;
    this.kennzeichen = kennzeichen;
}

Auto:

public class Auto extends Vehikel {

private String kennzeichen;
private int ps;
private double kW;
private boolean mayDriveOnH;
private double steuer = 0;

public Auto(int ps, String kennzeichen) {
    super(ps,kennzeichen);
    this.ps = ps;
    this.kennzeichen = kennzeichen;
}

Thanks.

  • 3
    The `ps` and `kennzeichen` variables in `Auto` hide the ones in `Vehikel`. Basically, they're different variables with the same names. Remove the ones in `Auto` (or remove them in `Vehikel`, if you don't really need them there). – Andy Turner Jan 26 '20 at 20:35
  • You've defined ps and kennzeichen twice, both in the abstract class and auto class. You'll need to use the protected keyword instead of private (Not sure what they java equivalent of protected is) in the abstract class and then you can remove the respective fields in the auto class. – BenKoshy Jan 26 '20 at 20:38
  • See [here](https://stackoverflow.com/a/56998844/11595728) for the difference between inheritance and visibility. – SDJ Jan 26 '20 at 20:39
  • @BKSpurgeon the Java equivalent of `protected` is `protected`. – Andy Turner Jan 26 '20 at 20:46

1 Answers1

0

I'm not familiar with Java, but something like this should put on the right track. Hopefully some java folks will correct any errors in the below.

The Code:

public abstract class Vehikel {

protected int ps; // change to protected
protected String kennzeichen; // change to protected
public final double uFaktor = 0.735;       

public Vehikel(int ps, String kennzeichen) {
    this.ps = ps;
    this.kennzeichen = kennzeichen;
}

public class Auto extends Vehikel {

// private String kennzeichen; // we have the protected field in the super class
// private int ps;  // we have the protected field in the super class
private double kW;
private boolean mayDriveOnH;
private double steuer = 0;

public Auto(int ps, String kennzeichen) {
    super(ps,kennzeichen);
    // this.ps = ps;   // You don't need this anymore because you can access the protected variable in the super class directly
    // this.kennzeichen = kennzeichen; // You don't need this anymore because you can access the protected variable in the super class directly
}

The Explanation:

As Andy Turner has said above, when you have the same variable in the abstract class and also the concrete Auto class, the variables in one hide the variables in the super/abstract class.

Basically, they're different variables with the same names

I can't say it better than that.

Use the protected keyword, so you can access the variables defined in the super class in the Auto auto class.

BenKoshy
  • 29,795
  • 12
  • 94
  • 75