For some reason I cannot access a method of a class, derived from another class

67 views Asked by At

I'm trying to create custom Matrix class in Processing to work with any number of dimensions. Class RotationMatrix derives from it. But I can't access methods of RotationMatrix, as if they didn't exist. My Matrix classes:

class Matrix{
  float[][] list;
  int[] dims;
  public Matrix(int cols, int rows){
    list = new float[cols][rows];
    dims = new int[] {cols, rows};
    
  }
  void Identity(){
    list = new float[dims[0]][dims[1]];
    for(int v=0;v<min(dims[0], dims[1]); v++){
      list[v][v] = 1;
    }
  }
  float[] mult(float[] vector){
    if(vector.length!=dims[0]) return vector;
    float[] result = new float[dims[0]];
    for (int i = 0; i<result.length;i++){
      float d = dot(vector, list[i]);
      result[i] = d;
    }
    return result;    
  }
  float dot(float[] v1, float[] v2){
    float result=0;
    for(int i = 0; i<v1.length;i++){
      result+=v1[i]*v2[i];
    }
    return result;
  }
  String toString(){
    String result="{";
    for(int y = 0; y<dims[1]; y++){
      if(y>0)result+=" ";
      result+="(";
      for(int x=0; x<dims[0];x++){
        result+=list[x][y];
        if(x+1<dims[0]) result+=",  ";
      }
      result+=")";
      if(y+1<dims[1]) result+=",\n\n"; 
    }
    result+="}";
    return result;
  }
}


class RotationMatrix extends Matrix {
  public float angle, scale;
  int ax1, ax2;
  int dim;
  public RotationMatrix(int dim){
    super(dim, dim);
    ax1=0;
    ax2=1;
    this.dim=dim;

  }
  
  public RotationMatrix(int dim, int a1, int a2){
    super(dim, dim);
    ax1=min(a1, a2);
    ax2=max(a1, a2);
    this.dim=dim;
    
  }

 
  void Update(){
    for(int y = 0; y<dim;y++){
      for(int x = 0; x<dim;x++){
        if(x==y) {
          
          if(x==ax1||y==ax2) {
            list[x][y] = cos(angle)*scale;
          }
          else {
            list[x][y]=1;
          }
        }
        else{
          if(x==ax2&&y==ax1){
            list[x][y]=-sin(angle)*scale;
          }
          if(x==ax1&&y==ax2) {
            list[x][y] = sin(angle)*scale;
          }
        }
      }
    }
  }
}

Now using Rotation Matrix:

m=new RotationMatrix(3);
m.Update();

And this call of Update method throws error "The function "Update()" does not exist". What am I doing wrong? I'm relatively new to Processing. Edit: marking "Update" as public didn't help, restarting Processing either.

1

There are 1 answers

3
Konrad Rudolph On

First off, you probably declared m as Matrix instead of as RotationMatrix. But Matrix has no Update method, hence the error. You need to declare the type of m as RotationMatrix to fix this.

Next, since you didn’t declare Update as public, it’s currently package-private. If you attempt to access it outside the package in which RotationMatrix was declared, you will get the error you’re seeing. Declare the method as public to fix this.

(The same probably goes for most of the methods declared in Matrix.)