As BufferedReader derive from Reader. But in the BufferedReader, it add some extra public methods : such as readLine(), Looking forward your comments thanks in advance
-
2What makes you think that violates LSP? – Jon Skeet Aug 26 '15 at 10:07
-
I had a misunderstand about LSP. I thought that LSP asking the sub class must not override the super class's method. – Eric Aug 28 '15 at 03:06
3 Answers
The point of the Liskov Substitution Principle is that any entity of a type, B
, more derived than another type, A
, can be used to fulfil any behaviour expected of an entity of type A
. In other words, if B
is a subclass of A
, an instance of B
should do everything expected of an instance of A
.
This way, a variable assignment like A a = new B();
will not cause any surprising behaviour. The instance of B
can be treated as an instance of A
, so the type system protects the program from unexpected behaviour.
If, however, A
has a method foo()
, and B
overrides this method with new behaviour not expected from an instance of A
, the above assignment, which is fine as far as the type system is concerned, can still lead to unexpected behaviour.
In the case of BufferedReader
, which extends Reader
, the BufferedReader
implements everything in Reader
as expected, and merely adds further behaviour. If we use the assignment Reader reader = new BufferedReader(...);
, the class is designed so that any code using the reader
object, an instance of Reader
, need not know what kind of Reader
(in this case a BufferedReader
) the instance is. The fact that more behaviour is available from the BufferedReader
class is irrelevant; here we only know (and only need know) that we have a working Reader
. In this way, it satisfies the Liskov Substitution Principle.

- 2,329
- 1
- 18
- 23
No. It is not against Liskov Substitution principle. Still it is type of Reader
. You can substitute it.

- 120,458
- 37
- 198
- 307
It is not, as long as you do not override any functionanlity but only extend it. Basically, the LSP is fulfilled, as long as you could just use your child class instead of the parent class and the whole application still works the exact same way.