Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Interface and interface methods

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we do not provide access modifier, interface takes 'default' access and interface methods 'public' access?

Why the difference? What sense does it make to have interface methods public when interface itself cannot be seen by other packages? Shouldn't both have been either public or default?
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi prafulla didnt get your answer. can you elaborate it?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can be situation where an Interface needs to be implemented by a class which is in different package as that of interface. But here the implementing class should have access to methods inside the interface.So we give public access specifier for class inside interface by default .
and
In implements clause , U can specify interface name with complete package extension (, which is not possible in case of methods) .
Ex:
class ABC implements com.ex.InterfaceEx

Thank You ,
Santosh Kumar.T
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is a contract of an interface that its methods are abstract and public
to be implemented by any class from any class hierarchy or extended by any
interface.

Sometimes you may require that a interface must only be accessed in its
package only, not outside that. But in this case too, you can't violate the
general contract of an interface of being abstract and public.

Suppose there is one interface that is public and that extends the interface
that has default accessibility. Now a new interface you have created
extending the existing one. This interface will be accessible from anywhere
, no package only bound. If the previous interface method were having
default accessibility as you guessed
, now you can guess what would
go wrong. Yes, violation of interface contract methods to be accessed.
Any non-abstract class that implements the new interface must implement all
the methods of both the interfaces. So all the methods must be public
accessible.

It is the reason why methods of interface are public and abstract always irrespective of the accessibility of the interface itself.



Thanks,
[ June 22, 2007: Message edited by: Chandra Bhatt ]
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic