• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't interfaces be final?

 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try



I get "Illegal combination of modifiers: interface and final" I always thought you could declare an interface to be final....

_steve.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you proved an interface cannot be marked final. Just as well since it would serve no useful purpose.
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would have lots of uses. Atleast logically, making an interface final would prevent anyone from extending it, but it would remain implementable.

_steve.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it is useful to make a regular class final to prevent extending classes from (for example) changing behaviour to circumvent security. But an interface has no behaviour to change! Like Rick says, I can't think of any useful purpose of making an interface final. Do you have a specific example in mind?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Steve: What benefit would such a distinction serve? People are barely aware that interfaces extend other interfaces (rather than implement them) anyway. So OK, let's pretend they can be educated. You're proposing that "final" should prevent an interface from being extended, but not from being implemented. Why? What's a scenario where this would matter? And assuming such a scenario exists, why couldn't someone just create an abstract class which "implements" the interface, without actually providing a concrete implementation? I'm not sure why this would matter; I'm just observing that the difference between extending and implementing is foggy at best. Either way you're creating a subtype, inheriting the previous declarations - and you may or may not get around to actually implementing them with a particular class. Adding additional arbitrary rules at this point doesn't seem to help, in my opinion.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's imagine that interface can be final.
final interface A {
void fooA();
}
If I want to create interface B inherited from A I can't do that. Ok. I'll define B as:
interface B {
void fooA();
void fooB();
}
Now let's define a class C,
class C implements B,A {
void fooA(){}
void fooB(){}
}

So where is benefit that A is final? Right, final makes sense when actual implementation can be inherited, but not just methods declaration.
 
Kuldeep Vaishnav
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is not interface's main objective is to provide abstract implementation of methods to the implementing class so that every class can have its own implementation? so if you make interface final,it can be defined but can not be implemented, would not it kill the very purpose of defining an interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic