Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent an interface from getting extended

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.

like image 816
JWhiz Avatar asked Aug 21 '10 15:08

JWhiz


2 Answers

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.

like image 200
Johannes Wachter Avatar answered Nov 12 '22 12:11

Johannes Wachter


There is no way to prevent extending an interface.

You could detect if someone has extended your interface at runtime using reflection.

like image 29
Mark Byers Avatar answered Nov 12 '22 13:11

Mark Byers