| Author |
Why can't interfaces be final?
|
Steve Simon Joseph Fernandez
Ranch Hand
Joined: Jul 17, 2005
Posts: 35
|
|
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
Joined: Sep 19, 2004
Posts: 531
|
|
|
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
Joined: Jul 17, 2005
Posts: 35
|
|
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
Joined: Apr 21, 2005
Posts: 410
|
|
|
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
Joined: Jan 30, 2000
Posts: 18670
|
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
D Rog
Ranch Hand
Joined: Feb 07, 2004
Posts: 471
|
|
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.
|
Get power of your iPod with MediaChest | Minimal J2EE container is here | Light weight full J2EE stack | My blog | Co-author of "Windows programming in Turbo Pascal"
|
 |
Kuldeep Vaishnav
Ranch Hand
Joined: May 23, 2004
Posts: 72
|
|
|
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.
|
Kuldeep
|
 |
 |
|
|
subject: Why can't interfaces be final?
|
|
|