Search code examples
javainheritancetostring

inherited overridden toString method does not use the proper attributes


I'm currently trying to understand inheritance better. Therefore I wrote a simple class to handle vectors and then wanted to create a class for 2D vectors that inherits from the Vector class. Here is the code for the Vector class:

'''

    public class Vector {

private double[] coordinates;

public Vector() {
    this.coordinates = new double[0];
}

public Vector(int dim) {
    this.coordinates = new double[dim];
    for(int i=0;i<dim;i++) this.coordinates[i] = 0;
}

public Vector(double[] values) {
    this.coordinates = new double[values.length];
    for(int i=0;i<values.length;i++) this.coordinates[i]=values[i];
}

public void set(double[] values) {
    for(int i=0;i<Math.min(this.coordinates.length, values.length);i++) this.coordinates[i]=values[i];
}

public double[] get() {
    return this.coordinates;
}

public double norm() {
    double sqSum =0;
    for(double i:this.coordinates) sqSum += i*i;
    return Math.sqrt(sqSum);
}

public int getDim() {
    return this.coordinates.length;
}

public double skalarprodukt(Vector other) {
    if(this.getDim()!=other.getDim()) return Double.NaN;
    double sp = 0;
    for(int i=0;i<this.getDim();i++) sp += this.coordinates[i]*other.coordinates[i];
    return sp;
}

public boolean isOrthogonal(Vector other) {
    if(Math.abs(this.skalarprodukt(other))<0.000001) return true;
    return false;
}

public void add(Vector other) {
    if(this.getDim()== other.getDim()) {
        for(int i=0;i<this.getDim();i++) this.coordinates[i] += other.coordinates[i];
    }
}

@Override 
public String toString() {
    String ret = "(";
    for(int i=0; i<this.coordinates.length;i++) {
        ret += this.coordinates[i];
        if(i<this.coordinates.length-1) ret+=", ";
    }
    ret+=")";
    return ret;
}

    }

'''

and here for the Vector2d class:

'''

    public class Vector2d extends Vector {

private double[] coordinates = new double[2];


public Vector2d() {
    this.coordinates[0] = 0;
    this.coordinates[1] = 0;
}

public Vector2d(double x, double y) {
    this.coordinates[0] = x;
    this.coordinates[1] = y;
}

    }

'''

Now, if I invoked the toString method for a Vector object, it does what it should (i.e. the Vector (1,1) appears as "(1,1)" ), but if I invoke it for a Vector2d Object, the returned String is always "()", as if the coordinate tuple was empty. When I add the toString() method into the Vector2d class (with copy and paste), however, it works fine.

Can anyone explain to me why that is and how I can make it work? Preferably without just copying the methods into the subclass.

Thanks


Solution

  • You private fields in Vector2d and Vector are independent. They are only visible in class which declared them.

    Instead of declaring new field, declare it as protected in your superclass:

    protected double[] coordinates;
    

    you will then be able to assign to it in your subclass:

    public Vector2d() {
        this.coordinates = new double[2];
        this.coordinates[0] = 0;
        this.coordinates[1] = 0;
    }