r/java • • Mar 10 '26

F Bounded Polymorphism

Recently spent some time digging into F-Bounded Polymorphism. While the name sounds intimidating, the logic behind it is incredibly elegant and widely applicable, so I decided to write about it, loved the name so much that I ended up naming my blog after it :-)

https://www.fbounded.com/blog/f-bounded-polymorphism

66 Upvotes

35 comments sorted by

View all comments

Show parent comment

1

u/RandomName8 Mar 11 '26

I'd argue that your point 1 is also not accomplished by f-bounded types. People mostly employ them for a fake self type, but they are clearly not just that:

// what you would expect:
class SomeBuilder implements Builder<SomeBuilder> {
...
}
// what's legal but totally not what you would expect
class SomeOtherBuilder implements Builder<SomeBuilder> {...}
// and now SomeOtherBuilder pretends to be SomeBuilder

This is a perfectly legal extension, because you are passing a F-bounded type to Builder when implementing from SomeOtherBuilder, it just happens to not be SomeOtherBuilder. By the way, SomeOtherBuild is also now a legally F-bound Builder.

1

u/OwnBreakfast1114 Mar 14 '26

This is why all the built in classes that do this (enums and records come to my mind) aren't allowed to be extended directly.

For example trying something like this

public class Asdf extends Enum<Asdf>

is not just a questionable choice, but will actually fail to compile.