wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface problem" Watch "interface problem" New topic
Author

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
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface problem
 
Similar Threads
Abstract classes instead of Interfaces
Interfaces vs. classes
overriding
interface is abstract?
confusion in k&b page 118