aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes access modifier of interface method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "access modifier of interface method " Watch "access modifier of interface method " New topic
Author

access modifier of interface method

Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Why methods of Interface are public not protected?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Hi tushartrue,

the method declarations of an interface are by default public abstract because it only makes sense when a class is able to implement these methods. It's not allowed and wouldn't make much sense if you could declare a private method for example because abstract private methods could never get implemented by any concrete class!

Marco
Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Hi Marco,i was asking about protected modifier?
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Its not perfect answer.

See,Interfaces are for imposing a rule among programmers.

How your Module will communicate with mine,its just exposing the interface to some system(i.e how to interact),not implementation.

Implementation could be anything as long as the interface ( i mean how to communicate) is same.

Interfaces are for standard way of Communication( there are lots of other things)

So,You shouldn't hide it right.

So, Interfaces methods must be public.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Again,
Protected is used when you're going for INHERITANCE.

Interfaces are mainly used for Supporting Composition over Inheritance.So, when you doing composition, you depend on public methods.


It doesn't make sense to have protected method,right.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Oh sorry... the protected modifier is not an option for interface methods. They can only be public and are implicitly public abstract whether you declare it with these modifiers or not!

@Pratap: You're right, I only explained the technical aspect of interface method declarations because I thought this is what tushartrue wanted to know. Of course you'll usually use a public interface to propagate your public API or contract of your code to other programmers.

Marco
Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Thanks Pratap and Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access modifier of interface method
 
Similar Threads
Home interface or Home Interface stub
what results byte code in java
Writing an Interface
Time Capsule
How To Call Two Different Classes From the Main Program Using the Same API?