Why do so many focus libraries make their classes public and not final?

Before turning to me with the obvious answer, let me ask a question and say that not all classes are subclassed.

  • A subclass should be a carefully thought out and not a standard property of all classes.
  • It can be argued that classes should be final by default and not final if really needed.

I can come up with many benefits for private package classes.

  • If the class is not for my viewing and just an assistant, I should consider the implementation detail, making it not publicly available, because it does not fit when I look for classes by name or interface implementations in my idea.
  • this also means that if everything except the exported interfaces publicly looks at the api when viewing jar library files, it becomes much easier, since there is less noise. I no longer see the internal elements of the implementation.
  • You do not need to worry about subclasses outside of your package.
  • For users, a practical advantage means that the methods they need are also likely to be grouped together in one interface / class, rather than scattered across them in other public classes, which means less cluter, and there are less places to search and fewer knobs also manage.

So why don't people use these opportunities?

+4
source share
6 answers

Languages ​​exist, for example. python in which everything is open and nothing is final. This does not mean that they live in hell, and the modules cannot be used and maintained. On the contrary.

Software development is complicated. We are in our minds many times. It can be fun, we pursue the ideal, regardless of practical advantages - if we have time. If you are the only person who is responsible for hundreds of classes as a perfectionist, you will go crazy.

+3
source

Basically, circumstances are changing. What, in your opinion, are no longer subclasses, may be required for the subclass later. Many more “fluffy” design options (such as final class graduation) are considered later when you find out that you really have to be subclassed.

Most API projects take into account extensibility.

+1
source

In terms of whether classes should be final or not by default, this is a much debated point for which there is really no “right” answer. Java decided to do this one way, C # the other, different people think that different languages ​​are right and wrong.

Personally, I like how classes expand by default if people want to make them final, and then they can, this is just a keyword. But allowing a subclass simply adds more flexibility in my view, of course, I don’t need to expand every non-final object under the sun, but if I want to add some degree of specific behavior, then this option is there for me. Yes, a composition can be used (and indeed should be used in some cases), but this does not exclude and should not exclude a subclass as an option. There are many cases where a subclassified object fulfills an is-a relationship with a parent, in which case this is the right way. Of course, although this has been misused in the past, even in the Java API (properties, for example, are not really a hash table.) But if you try to change the language so that people stop being stupid, they will always find a way around it!

As for making the class package private, for me this option would be much more useful if the packages were hierarchical rather than flat. Yes, they are hierarchical in description, but something declared by the private package in org.me cannot be obtained from org.me.subpackage, which I often feel the need to do. Fortunately, with the addition of superpackages, we can better use this option!

+1
source
  • Your perceived benefits of libraries that do not support a subclass are not supported by my experience.

  • Java has no delegation; subclasses are often used as adapters or method hooks. It is more readable to do this in code than to do it using a reflective proxy.

Things are changing. It’s easier to work with. This sometimes requires drastic enhancements, such as the default Java 8 methods. These neatly set up methods for adding to interfaces after interfaces that are widely used.

+1
source

Well, that’s just because thinking that SHOULD NOT be expanded requires a lot of effort.

For most small and medium-sized components and frameworks, it is reasonable to assume that depreciating and preserving the abused / overused method / class is easier and more convenient than thinking about the most likely and some very unbelievable cases and still sometimes shutting down things that need dirty delegation then / decoration instead of a simple extension.

In my practice, only Java Spring really bothered me with such a proactive closure (which leads to a significant clear API after a few micro releases that I think).

I had a quote from R. Martin on this issue:

http://java.akraievoy.org/2009/03/openclosed-principle-strategic-closure.html

+1
source
  • modifiers public and final from different categories , putting them in one sentence, is not a good start when you want to clarify the situation.
  • as for the final change in requirements , and after a while you may need a subclass for the final class, plus not all libraries / frameworks can drawer boxes with final classes
  • how to access packages - it can make refactoring more difficult , but yes, if you create some library in order not to publish inner classes, this is a good "template"
0
source

Source: https://habr.com/ru/post/1336390/


All Articles