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.
Joined: Nov 26, 2000
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
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
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
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.