aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Methods 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 "Interface Methods" Watch "Interface Methods" New topic
Author

Interface Methods

vijay malhotra
Greenhorn

Joined: Nov 26, 2000
Posts: 26
Why the methods that implement an interface must be declared public ?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vijay,
Quite simply it is because an "Interface" is a contract telling other classes that you have implemented some method. That method must be available to any other class in the JVM otherwise the contract is not very good. By using any other access mode, we would be limiting classes that could call the contract method. This would cause major problems in say AWT where listener interfaces are rampant.
Regards,
Manfred.
vijay malhotra
Greenhorn

Joined: Nov 26, 2000
Posts: 26
Hi Manfred
Thanks for replying. But I still have doubt.I was not able to understand clearly.
But what if we still want that a method of an interface should be kept as private method for the class that implements the interface ? Will the compiler will report an error ? If yes why?
Regards
Vijay
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by vijay malhotra:
Hi Manfred
Thanks for replying. But I still have doubt.I was not able to understand clearly.
But what if we still want that a method of an interface should be kept as private method for the class that implements the interface ? Will the compiler will report an error ? If yes why?
Regards
Vijay

Interface is probably one of the best features of Java to avoid multiple inheritence. It is yuckky in some way to inherit the implementation from multiple parents..
Now.. coming to your question. Manfred explains it very clearly.
All the interface methods are by default public. They cant go lower on their accessibility during implementation.. gives a compile time error
HIH
Ragu
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Vijay,
An interface is just a means of declaring behaviour. It's up to the user to work out how the behaviour will be implemented.
If you could declare a private method in an interface it would have to be implemented in the interface ... private methods are not inherited. You would be telling anyone using your interface that the behaviour 'must be' implemented in a specific manner.
And that would totally defeat the purpose of using an interface in the first place
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Thinking about the words of Manfred: An interface is a contract. private methods are not part of the contract because they can not be called from the outside world.
Maybe it wouldn't make sense to have a contract callable only from the same package (if we could place default access for the methods in an interface) because of the way how classes are intended to be made: the developer of some classes provide a package that the client programmer (the programmer who uses them) is supposed to import. The classes that utilize the library are not in the same package as the clases in the library.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Methods