In Java, Is there a way to prevent extending an interface.
Since final
cannot be added to an interface
, i was curious to know whether there was a way if any, to prevent extending an Interface.
The problem here is a conceptual one. Interfaces are meant to be extended because they can't be used as-is.
What you could do is put the interface inside a package and give it default visibility. With that only classes inside that package can implement that interface.
But with that solution you also lose the possibility to use the interface outside that package, so it is no real solution.
There is no way to prevent extending an interface.
You could detect if someone has extended your interface at runtime using reflection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With