Liskov principle and Matrix design

Viewed 21

I'm trying to design following classess: Matrix and SquareMatrix. Both of them will be able to do basic matrix operations and SquareMatrix will additionaly be able to check if it is diagonal. So, I was thinking of creating the Matrix class and then have SquareMatrix inherit from it. Is it a good idea considering Liskov principle? Because at first it seems like the basic violation with square and rectangle but when I think about it I cant find any reason why it would be a bad idea. Can someone point me in the right direction?

1 Answers

It depends.

If SquareMatrix just adds one extra method to check whether it is diagonal, and all other methods can remain exactly as they where, then it might be OK. Adding methods in a subclass to an inherited class is generally compatible with LSP.

But most likely, you would also want to insist that the matrix is, in fact, square. So if your original Matrix class has functionality to change the height or the width of the matrix, you would have to somehow 'disable' this functionality in the SquareMatrix subclass. And such disabling is incompatible with LSP.

If you wish to have a separate SquareMatrix class, then the normal route would be a MatrixInterface with an API that all matrices have in common, an AbstractMatrix providing a basic implementation of this interface, a ResizableMatrix for matrices of which the dimensions can change, and a SquareMatrix with height and width guaranteed to be equal -- where the latter two both inherit from the AbstractMatrix.

If this is overly complex for your use case, a cleaner solution might be to add two methods to your Matrix class: isSquare, which returns whether or not a matrix is square, and isDiagonal, which under the assumption (precondition) that the matrix is square returns whether the matrix also diagonal.

Related