Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why public?

 
Shravan Durvasula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all:
This is one of my interfaces:

This is a class that implements the interface:

The compiler is asking me to make the implemented method (methodB in MainClass) public.
But in the original interface (InterfaceA) the scope of methodA is package. Then why do i need to make it public in the implemented class?
Thanks and Regards,
-skd
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS, §9.4 Abstract Method Declarations:

Every method declaration in the body of an interface is implicitly public.

Corey
 
Shravan Durvasula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey.
"How can i have an interface whose methods can be implemented only within the package?"
Does the above question make sense because now i understand that everything in an interface is public?
Thanks,
-skd
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shravan Durvasula:
"How can i have an interface whose methods can be implemented only within the package?"

You can't. All methods defined within an interface are implicitly public. It is a compiler error to override a method with a more restrictive access modifier.
If you want that sort of functionality, you'll have to extend a class that has methods assigned the "default" access modifier.
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic