Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

interface - Another Complete Reference  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface are implicitlt abstract

because they are not concrete or they dont have any concrete method .

methods in interface are implicitly public & abstract

abstract is okey because it is not concrete .
but why should public ???

variable in interface are implicitely public, static & final

why should public ???
why should static ???
why should final ???

can any body provide some good & logical answers ...

thank you very much .
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interface methods are inherently public because the definition of an interface is a declaration of public methods that an implementing class must implement. I know there's a better way to explain that, but it's not coming to me just now.

Interface members must be static (non-instance) to side-step the thorny issues involved in implementing multiple inheritence. They're public for the same reason above, and they're final because all static members are by nature final.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:
Interface members ... They're public for the same reason above, and they're final because all static members are by nature final.


Static members are not necessarily final. Think of an interface as a contract to provide certain services. Any class that implements the interface must honor the contract of the interface. If an implementing class could change the interface member's value, it would thereby break the contract. To prevent this from happening, values of interface members cannot be changed, i.e. they are final.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not too much clear ...
you just made a point not an explanation .

but thanks .

can any other person make me clear about this ...
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:

methods in interface are implicitly public & abstract

abstract is okey because it is not concrete .
but why should public ???


Interfaces are meant to define the public API of a class library. Having non-public methods in them is counter to that design goal.


variable in interface are implicitely public, static & final

why should public ???
why should static ???
why should final ???

can any body provide some good & logical answers ...

thank you very much .


See above
The designers of Java advocate rather strict OO practices. Having public non-final fields is counter to that.
Having non-static final fields is useless as well as they'd have the same value for each instance, thus wasting memory.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Junilu Lacar:
Static members are not necessarily final.
Yeah I didn't think that one through too well. All static methods are final by nature. Clearly this doesn't apply to static fields. Oops!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All static methods are final by nature.


A superclass's non-final static method can not be overridden but can be hidden by a subclass's static method. A final method can be neither overridden nor hidden in a subclass.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:
A final method can be neither overridden nor hidden in a subclass.
Really? I could swear I recall reading this in one of Sun's specs or docs a ways back. Then again, I've done a lot of reading over the years.

Double apologies for the confusion. Time to do a little work, I think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!