0

When programming, there are many indicators that coupling is bad. A class should know as little as possible about other classes. So it is modular and can easily be replaced.

Now, with the introduction of sealed classes, the abstract super-class knows about its sub-classes. As I understand it, the sub-classes would normally be in the same package (or even the same file) as their sealed interface. So there should not be a problem of cyclic dependencies between packages.

So I guess what I am asking is: Should a sealed interface and its sub-classes be regarded as one unit, and not as modular parts that are dependent on each other?

Example where the sub-classes are outside the package:

import asdf.Car;
import asdf.Truck;

public sealed interface Service permits Car, Truck {

To trigger-happy close-voters: An implementor of a sealed interface cannot exist outside the interface's module so the answer is pretty cut and dry. Not opinion-based at all. Here is a comment from Brian Goetz that you might be interested in: Sealed classes for classes in different packages

I already got my answer though so I don't really care if no one else can answer. Have a nice day!

  • 1
    Just a side-note: It's generally not referred to as a subclass if it `implements` an `interface`. That's just a regular *class* or an *implementation*. If `Service` was an `abstract class` then you would say *subclass* – vakio Aug 07 '21 at 09:27
  • 1
    "*When programming, there are many indicators that coupling is bad. A class should know as little as possible about other classes. So it is modular and can easily be replaced.*" - Careful. It is not our goal to decouple everything from everything. That would be over-engineering. Our goal, among other things, is to decouple at the right boundaries. In a conceptual context, coupling is not a bad thing. It should, however, be choosen consciously. – Turing85 Aug 07 '21 at 09:34
  • @Turing85 You're right, that was poorly worded. It might be the norm of overusing inheritance that I was trying to combat. But of course there are many valid uses for coupling too –  Aug 07 '21 at 10:23
  • @vakio Yeah, I didn't mean subclass specifically, just something below that "takes from above". But of course it is important to use the correct terminology –  Aug 07 '21 at 10:30

1 Answers1

0

Inheritance is always strong coupling between types; hence most often you should follow

Favor composition over inheritance

Most of the cases when you use inheritance could be resolved with composition and dependency injection.


Keeping subclasses close to the base class inside the one module is a good practice and doing otherwise is not recommended. You don't want to have a strong coupling between not related packages or modules.


There are exceptions to everything I said. F.e You might want to create a library of abstract classes than developers in your project could extend without duplicating utility code. F.ex java collections and abstract collection classes. .

Marek Żylicz
  • 429
  • 3
  • 8
  • 1
    So if I want to use sealed classes: don't spread them out. And if I don't want to use sealed classes: it's OK (sometimes). And inheritance is by nature strong coupling and so I shouldn't be overly concerned about coupling anyway here. –  Aug 07 '21 at 09:55
  • Yes. Definitely, when you use inheritance you already create coupling so use it only when reasonable. In general, try to create a code that is highly cohesive, lowly coupled. In other words, it means that classes that should change together (same responsibility) are close to each other, and classes that can change separately are in different packages. – Marek Żylicz Aug 07 '21 at 10:12
  • 1
    I found something like the following "A sealed class imposes three important constraints on its permitted subclasses: 1. ll permitted subclasses must belong to the same module as the sealed class. 2. Every permitted subclass must explicitly extend the sealed class. 3. Every permitted subclass must define a modifier: final, sealed, or non-sealed. So an intent of sealed classes seems to keep them close to the base class ;-) – Marek Żylicz Aug 07 '21 at 10:13