3

As since in a class I can do:

public final class Foo{}

wich means no more classes can extends that Foo class... e.g. String class is final, so no custom class can extends the class String.

How can I prevent to do the same with an interface?

If I do

public interface ISome{
    void fly();
}

I would like to allow that

class A implements ISome {}

but block that

public interface IHouse extends ISome{
    void fly();
}

doing this

public final interface ISome{}

makes no sense... and will bring a compile error like:

Illegal modifier for the interface
user207421
  • 298,294
  • 41
  • 291
  • 462
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91

1 Answers1

4

You can't.

Supposedly the Java designers didn't think there would ever be an appropriate use case for this: if you don't want an interface to be extended then really you ought to declare those functions directly in a concrete class.

That said, you can achieve this in C++ as in this language an interface is more of a convention - consisting of only pure virtual functions, and you can enforce non-extensibility with techniques such as friendship.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470