Reputation: 681
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.
Upvotes: 7
Views: 1150
Reputation: 10891
This is very similar to a previously asked question -- final interface in java.
I gave two answers.
In addition, you can put the interface inside a class, give it the private access modifier, and make sure no interface in that class file extends it.
Upvotes: 0
Reputation: 2735
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.
Upvotes: 3
Reputation: 838146
There is no way to prevent extending an interface.
You could detect if someone has extended your interface at runtime using reflection.
Upvotes: 2