Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface problem

 
harsh garg
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all know methods in interface are public in nature , abstract,include only declarations and in interface there is no coding part of method in interface .Coding part of method is included in classes ,here in classes methods are always prefixed with "Public" Keyword. If we not include the public keyword it generate error why this so?
And my question is methods in interfaces are public by default,so why should Prefixed a public keyword in classes?
Please help me ......
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in classes, leaving off an access keyword does not default to making the method public, which it must be in order to implement the interface.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead it gets "default" access. Check out this link.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For classes if the access specifier is omitted it gets default or package private access.

But for Interfaces the methods must always be public. Even if you leave off the access specifier the method is public! Check the compilation error on this code.




javac Interfaces1.java

Interfaces1.java:10: runAround() in Sprint cannot implement runAround() in Inter
faces1; attempting to assign weaker access privileges; was public
void runAround()
^
1 error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic