Answer the question
In order to leave comments, you need to log in
How is the open / closed principle implemented in the case of "branching" extensions in Java?
There is an entity that needs to be expanded. Based on all the extension methods that are specified in the OCP, the main one is inheritance. If this entity is expanded in only one direction, then everything is fine. But if this entity needs to be expanded in different directions, what should be done? There is no multiple inheritance in Java.
Example: There is a class - Person.
After some time, they will want to expand this class according to OCP - they will inherit it and make the Bartender class.
But what if later on the class Man wants to be expanded in another direction? For example, the Astronaut class, also inherited from the Human class. Wherein, if you want the Person to be both the Astronaut and the Bartender at the same time, this will not work due to the fact that the Bartender and the Astronaut will already require separate instances, which corresponds to two different people.
Answer the question
In order to leave comments, you need to log in
the main one is inheritance.
class Human
{
Profession[] professions;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question