Junior — Middle
Which SOLID principle is violated if the behavior of a inherited class is extended by adding a new method?
sobes.tech AI
Answer from AI
When a new method is added to a subclass during behavior extension, it can violate the Open/Closed Principle from SOLID. This principle states that classes should be open for extension but closed for modification. Adding a new method in a subclass is not always a violation by itself, but if it leads to changes in existing behavior or requires modifications to client code, then the principle is violated.
It is also important to consider the Liskov Substitution Principle: a subclass should be interchangeable with its base class without affecting the correctness of the program. If the added method changes the contract or behavior, it may violate this principle.