javarubiks-cube

Creating a child class of Rubik class called RubikRight


I have a Rubik class with the following:

public class Rubik{

    private int[][][] grid;
    protected Face[] cube;
    protected Face up;
    protected Face left;
    protected Face front;
    protected Face right;
    protected Face down;
    protected Face back;
    private static String dots = "......";
    private String output;

    //Constructor
    public Rubik(int[][][] grid){
        int[][][] copy_grid = new int[6][3][3];
        for (int k = 0; k < 6; k++){
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++)
                copy_grid[k][i][j] = grid[k][i][j];
            }
        }
        this.grid = copy_grid;
        this.up = new Face(grid[0]);
        this.left = new Face(grid[1]);
        this.front = new Face(grid[2]);
        this.right = new Face(grid[3]);
        this.down = new Face(grid[4]);
        this.back = new Face(grid[5]);
        this.cube = new Face[]{this.up, this.left, this.front, this.right, this.down, this.back};
    }

And I am trying to create a RubikRight class which extends the Rubik, and RubikRight is orientated in such a way that the right face of the original Rubik is now facing front. This is how I am defining my constructor for RubikRight:

public class RubikRight extends Rubik{

    //Constructor
    public RubikRight(int[][][] grid){
        int[][][] copy_grid = new int[6][3][3];
        for (int k = 0; k < 6; k++){
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++)
                copy_grid[k][i][j] = grid[k][i][j];
            }
        }
        this.grid = copy_grid;
        this.up = new Face(grid[0]);
        this.left = new Face(grid[2]);
        this.front = new Face(grid[3]);
        this.right = new Face(grid[5]);
        this.down = new Face(grid[4]);
        this.back = new Face(grid[1]);
        this.cube = new Face[]{this.up, this.left, this.front, this.right, this.down, this.back};
    }

However, I am getting the error which says

Constructor Rubik in class Rubik cannot be applied to the given type;
  public RubikRight(int[][][] grid){
                                     ^
  required: int[][][]
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

May I know why is it that I can't seem to define RubikRight that way?


Solution

  • Whenever you a instantiate a child class object the parent class default constructor is called implicitly. So, when you instantiated RubikRight by calling new RubikRight(int[][][]) it implicitly called the super() from inside the RubikRight's constructor. And hence the error:

    required: int[][][] // what you have in Rubik class, note that you don't have the default constructor
    found: no arguments // super(/* no argument */) called implicitly
    

    To remove the error, you have two options:

    1. Either call super(grid) from RubikRight constructor explicitly.
    2. Or, implement the default constructor in the Rubik (base) class.