This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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 ]
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic