Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why can't interfaces be final?

 
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.
 
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.
 
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?
 
Wanderer
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.
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • 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.
 
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.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!