I am using EJML and I want to use the class LinearSolver_B64_to_D64
, which has the constructor: LinearSolver_B64_to_D64(LinearSolver<BlockMatrix64F> alg)
with the interface LinearSolver<BlockMatrix64F>
and the class has already implemented the LinearSolver
.
What I know: In general you create a interface, than you would implement that interface in a specific class. I read about functions(in the specific class) that take the interface as a parameter, because that way your functions does not need to know something about the class.
My question:
I don't know how to initialise the class LinearSolver_B64_to_D64
, because I don't know how to pass an interface as a parameter.
Update: I tried the following code:
public class UseMatrixInterface{
public UseMatrixInterface(){
}
public void do1(){
DenseMatrix64F a = new DenseMatrix64F(3,3);
LinearSolver_B64_to_D64 ls = new LinearSolver_B64_to_D64(null);
//it throws a nullpointer exeption. I assume, it is because i used null
//instead of the requiered parameter.
ls.invert(a);
a.print();
}
public void do2(){
LinearSolver<BlockMatrix64F> lsD;
LinearSolver_B64_to_D64 ls = new LinearSolver_B64_to_D64(lsD);
//not working, because lsD cannot be initialised;
}
}
Just study javadoc; like starting here for the interface LinearSolver.
And guess what: there is a section
All Known Implementing Classes: AdjLinearSolverQr_D64, ...
Go and pick the one that matches your needs; and create an instance of that class.
So the answer to your question is: you can't instantiate interfaces. Instead you look out for classes implementing the interface, and then you create an instance of such a class. Like in: