3 ms·
> I rarely see inheritance used in practice in java code bases. That seems absurd to me and I have a hard time understanding it, honestly.
by insanitybit 3y ago
> I rarely see inheritance used in practice in java code bases.
That seems absurd to me and I have a hard time understanding it, honestly.
- wayfinder 3y agoIf you type everything with interfaces in your codebase, you are much less tied to inheritance. In fact, everyone could be written to be composed. However Java doesn’t support type union so you can get into some ugly and verbose situations but the JVM doesn’t really check type so this is more a compile-time issue and could be fixed in a future Java language revision.
- logicchains 3y ago>However Java doesn’t support type union so you can get into some ugly and verbose situations Isn't the "sealed interface" described in the OP blog post a type union? Or you mean anonymous unions?
- wayfinder 3y agoDefinitely referring to anonymous unions too. Without them, there’s still friction sometimes which makes union types unnatural. I haven’t written Java in a while and I can’t remember if you could sometimes fake a type union using a generic type on a method, but if you can, it’s definitely super ugly and would raise eyebrows during any code review.
- logicchains 3y agoAnonymous unions are a relatively rare feature; Rust and Haskell don't have them, for instance.
- Supermancho 3y ago> I rarely see inheritance used in practice in java code bases. Without some specific call-out, it can be assumed that this is a very niche viewpoint that has no bearing on modern development. The vast majority of projects use inheritance, today. Does it use Spring? extends SpringBootServletInitializer Meaningful responses using values from a request? extends OncePerRequestFilter Formatting exception handling responses on web requests? extends ResponseEntityExceptionHandler Then there's all the interfaces you have to satisfy, because it's all tied into the standard library and popular libraries.
- richardw 3y agoYou need to inherit to create anything that is a class. But the focus is on composition. You inherit from useful classes so you can build the solution using composition. I don’t like modern Java because there’s too much non-Java magic code. Layers of stuff that “helps” but removes me from the language. Entire programs written in config and special text wrapping classes and methods. How it works requires understanding multiple intersecting languages that happen to be strung together in .java files. Edit: when something is in config it’s not checked at compilation. Every environment from dev to prod can have its own config so when you compile in dev you don’t know what’ll happen in prod. I know: let’s add more tools and layers.
- Supermancho 3y ago> You need to inherit to create anything that is a class That's true of the Java compiler, for which the documentation is strewn around the intenet...but an example is here: https://medium.com/javarevisited/compiler-generated-classes-methods-and-fields-in-java-343dde2cce61 https://medium.com/javarevisited/compiler-generated-classes-... The Java syntax doe not require extending Object explicitly.eg This is a valid, useless, class: public class App {} > I don’t like modern Java because there’s too much non-Java magic code It seems like this is the common path for popular languages. They develop their own library-backed DSL's for the most common use cases, which are often little more than macros (@data @getter, @notnull, etc). I am biased by what I've seen in the last 30 years though.
- chii 3y agoThe OP/GP wasn't really complaining about inheritance, despite the fact that this is what they wrote. The OP is complaining about the difficulty of the API because it is _exposed_ through inheritance. The complaint about `SpringBootServletInitializer`, for example, is exactly this. There's nothing wrong with inheritance. In fact, SpringBootServletInitializer is exactly what you want to use inheritance for - because you need to build your app using the servlet api. There are http servers that aren't servlet api, such as https://vertx.io/docs/vertx-web/java/#_re_cap_on_vert_x_core_http_servers https://vertx.io/docs/vertx-web/java/#_re_cap_on_vert_x_core... , which uses little to no inheritance (since the api surface is smaller).