What case of inheritance will be correct?
When we inherit rectangle from square or square from rectangle or we shouldn't do inherit in this case?
Asked
Active
Viewed 96 times
0

Alex K.
- 18
- 2
-
2What about inheriting both of them from Class *Shape*? – Muhammad Faizan Uddin Dec 25 '16 at 00:20
-
3As you probably know, square **is** always a rectangle, but rectangle does not have to be a square. – PM 77-1 Dec 25 '16 at 00:23
-
https://en.wikipedia.org/wiki/Is-a – dm03514 Dec 25 '16 at 01:50
-
http://stackoverflow.com/a/584732/414076 Listen to Uncle Bob. Or at least someone summarizing Uncle Bob. – Anthony Pegram Dec 25 '16 at 06:33
2 Answers
1
mathematically, a square is a rectangle.
but oo-wise, a square is not a rectangle because it will break the liskov substitution principle.
see also the circle ellipse problem.

Ray Tayek
- 9,841
- 8
- 50
- 90
-1
Yes As per "Muhahhad Faizan Uddin" said square is always a rectangle, but vice versa is not always true. But if can see square if more of specific shape and rectangle is generalized term. So In my experience Square should Inherit rectangle class.

Rinku saru
- 1
- 2