File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes interface problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface problem" Watch "interface problem" New topic

interface problem

harsh garg
Ranch Hand

Joined: Jul 13, 2008
Posts: 33
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

Joined: Jan 10, 2002
Posts: 63839

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.

[Asking smart questions] [About Bear] [Books by Bear]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Instead it gets "default" access. Check out this link.

How To Ask Questions How To Answer Questions
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
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 runAround() in Sprint cannot implement runAround() in Inter
faces1; attempting to assign weaker access privileges; was public
void runAround()
1 error
I agree. Here's the link:
subject: interface problem
It's not a secret anymore!