• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface Methods

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the methods that implement an interface must be declared public ?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
eat bricks! HA! And here's another one! And a tiny 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