File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default access modifier of interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default access modifier of interface" Watch "Default access modifier of interface" New topic
Author

Default access modifier of interface

Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
I have declared following interface.

interface b
{
public void g();
final static int i=100;
}

Q1:- What will be the access modifier of this interface? Default or public. I understand that if I declare class without access modifier it will be declared as default.

Q2:- Also what will be the access modifier for variable i? Default or public?

Thanks in advance.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
hi Nilesh ,

The default access modifier for a method in an interface is always public and for a memeber variable it is public staic final

interface b
{
public void g();
final static int i=100;
}

and one thing more Java does not commend redundancy hence inspite of writing
public void g();

just write
void g();

and inspite of this final static int i=100;

write
int i=100;

Hope i expalined u
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
Hi if you dont specify access modifier for interface by default it is "public" because all the methods dont have any implementation , it is necessary that other classes implement interface and provide implementation to methods.

variables are public static final


Kiran Kumar.
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

interface b
{
public void g();
final static int i=100;
}

Q1:- What will be the access modifier of this interface? Default or public. I understand that if I declare class without access modifier it will be declared as default.

The access modifier for this interface is "default". "public" keyword is required if you want to make your interface accessible to everyone.

Q2:- Also what will be the access modifier for variable i? Default or public?

For all interface variables the access modifier is : "public static final" whether you metion it or not.

For all interface methods te access modifier is "public abstract", again whether you metion it or not.

There some other points that you must know about interface. Check K&B1.5 page 20 & 21.

Asha


SCJP 1.5
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[faisal]: The default access modifier for a method in an interface is always public

The methods and fields within the interface are public (not just by default, but always, period, no exceptions). The interface itself is package access by default. Since Q1 asked about the interface (not a method or field) then the answer to Q1 should be package. Q2 asks about the field i, which is public. No one asked about method a(), but it's public too.


"I'm not back." - Bill Harding, Twister
Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
Thanks all. Last note by Jim is correct.
 
 
subject: Default access modifier of interface