Introduction
I am working on my master's thesis on inheritance issues and indicators that show that there is an inheritance problem.
As in the following example:
Example
public static String getAnimalNoise(Animal animal) { if (animal instanceof Dog) return "Woof"; if (animal instanceof Cat) return "Miau"; return ""; }
The method returns String "Woof" if the given instance of Animal is Dog and "Miau" if it is Cat . An empty line, because some animals do not make noise at all.
Therefore, the correct solution for this should be to use polymorphism using the getNoise method in the Animal class.
I have analyzed various indicators of inheritance problems and I want to say if some of them violate the SOLID principle.
I thought the above example violates:
- Single Responsibility Principle (SRP)
- Open / Closed Principle (OCP)
- Liskov Substitution Principle (LSP)
- Dependency Inversion Principle (DIP)
But I'm not sure if this is true for everyone.
I thought:
VIOLATIONS OF THE PRINCIPLE
SRP violation
Because conditional statements generally violate SRP, because, like a case case switch or more than one if-else statement, more than one responsibility is considered.
There are two cases, so there are several reasons for changing the method.
OCP violation
Because if a new animal is added, a new method must be added to the method so the method is not close to changes.
LSP VIOLATION
Each branch performs different actions depending on the subtype of animals. What do I consider violating LSP ?! I know an example of a rectangle and a square and getArea, but these for example, I thought it was suitable for a violation.
VIOLATION DISPLAY
Conditional operators take a dependency, which means that the operators depend on the details, not on the abstractions that violate DIP.
Question:
So, the question is, what are the true principles for this example, the truly violated and correct arguments?