3

i am trying to create a class system like this:

public class Matrix {
    private int[][] m;
    public Matrix(int rows, int cols) {
        //constructor
    }
    public int get(int row, int col) {
        return m[row][col];
    }
}

public class Vector extends Matrix {
    public Vector() {
        //constructor
    }
    public int get(int index) {
        return super.get(0, index);
    }
}

I want the Matrix.get(row, col) function to be public, but i don't want it to be public through the Vector class. I don't want this to be possible:

Vector v = new Vector();
int x = v.get(1, 1);

The private access modifiers doesn't help me, because it doesn't make the method available outside of the Matrix class (except for its inheritors)

Any ideas on how it could be done?

11
  • 5
    In that case, Vector shouldn't extend Matrix. Why do you want to achieve something like this? Sounds like an XY problem. Commented Mar 24, 2017 at 18:59
  • how about protected ;). try it and see Commented Mar 24, 2017 at 18:59
  • Its not possible. But you can do one thing you can override the get method in Vector class and deprecate it. @Override @Deprecated /** * do not use method */ public int get(int row, int col) { return -1; } Commented Mar 24, 2017 at 19:06
  • @Sagiv can you tell us what is the reason behind that you don't want Vector's object not to see the parent's get method? I mean what is the specific problem you are trying to solve? Commented Mar 24, 2017 at 19:09
  • 1
    You can try change the package structure and use default access modifier. Commented Mar 24, 2017 at 19:10

3 Answers 3

1

Unfortunately, that's not possible because if a class inherits another, you must be able to call all of the methods of the class you are inheriting.

If you don't want to be able to do this because the index will be out of bounds, then add a getRows() and getColumns() method to matrix and anyone who has an instance of a Vector will check to make sure when they call get(int row, int col) it won't throw an index out of bounds exception.

Sign up to request clarification or add additional context in comments.

Comments

0

How about

public class Vector extends Matrix {
  public Vector(int cols) {
    super(0, cols);
  }

  public int get(int index) {
    return get(0, index);
  }

  @Override
  public int get(int row, int col) {
    if (row > 0) {throw new IllegalArgumentException("row != 0");
    return super.get(0, index);
  }
}

?

Comments

0

In this case you could think to use composition over inheritance The Vector class will become:

  public class Vector {
    private Matrix matrix;
    public Vector() {
      matrix = new Matrix();
    }
    public int get(int index) {
      return matrix.get(0, index);
    }
  }

Another solution could be to invert the inheritance:

 public class Matrix  extends Vector{
    private int[][] m;
    public Matrix(int rows, int cols) {
      super(0);
    }
    public int get(int row, int col) {
      return m[row][col];
    }

    @Override
    public int get(int index) {
      return m[0][index];
    }
  }

  public class Vector {
    private int[] v;

    public Vector(int length) {
      v = new int[length];
    }
    public int get(int index) {
      return v[index];
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.