Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default access modifier of interface

 
Nilesh Deshpande
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Asha Pathik
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Nilesh Deshpande
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. Last note by Jim is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic