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 Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
Can the interface be private and protected ?
-Arsho
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
An interface cannot be declared private or protected, the only modifiers allowed are public and abstract. But both of these modifiers are specified by default, so declaring an interface public and/or abstract would be redundent.


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Top-level interfaces can only have public or default access modifiers.
Nested interfaces (contained within another class) can have any access modifier.
Rob


Rob
SCJP 1.4
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Sorry and interface is declared abstract by default and all methods are implicitly public and abstract, also all fields are public static final by default.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Only interfaces defined inside another Class can be private or protected.

Originally posted by Arsho, Ayan:
Can the interface be private and protected ?
-Arsho
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Rob, can a nested interface be declared final?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Rajinder, no, interfaces are never final, they're always implicitly abstract, because they only define an API, but never provide an implementation.
Rob
[ January 23, 2002: Message edited by: Rob Ross ]
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
final does not make sense for an interface.
final modifier is for class, method and variable

Originally posted by Rajinder Yadav:
Rob, can a nested interface be declared final?
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

Top-level interfaces can only have public or default access modifiers.
Nested interfaces (contained within another class) can have any access modifier.
Rob


Only interfaces defined inside another Class can be private or protected.

These two responses are conflicting, can Rob or someone please verfiy?
-Paul
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
no they are not conflicting.
what i had meant was that protected and private can only be used for a interface if it is inside another class. ofcourse this interface can have public and default-access modifier too.
Originally posted by Paul Salerno:

These two responses are conflicting, can Rob or someone please verfiy?
-Paul

[ January 23, 2002: Message edited by: mark stone ]
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Mark I guess I was trying to point out the use of the all incompassing term "any" in Rob's response, a point that may confuse other readers.
>>Nested interfaces (contained within another class) can have any access modifier.
A interface is abstract be default, so you're right that abstract and final cannot be used together.
Originally posted by mark stone:
final does not make sense for an interface.
final modifier is for class, method and variable


[ January 23, 2002: Message edited by: Rajinder Yadav ]
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Mark I guess I was trying to point out the use of the all incompassing term "any" in one of the responses, a point that may confuse other readers.
>>Nested interfaces (contained within another class) can have any access modifier.
An interface is abstract by default, so you're right that abstract and final cannot be used together.
[ January 23, 2002: Message edited by: Rajinder Yadav ]
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Mark I guess I was trying to point out the use of the all incompassing term "any" in Rob's response, a point that may confuse other readers.
>>Nested interfaces (contained within another class) can have any access modifier.
An interface is abstract by default, so you're right that abstract and final cannot be used together.
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

no they are not conflicting.
what i had meant was that protected and private can only be used for a interface if it is inside inner class. of course this interface can have public and default-access modifier too.

I still dont understand what is meant by this. If anyone has a code example, I would love to see it.
TIA
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
public class Test {
//somecode here
private interface hello {
}
}
now interface hello can have any modifier.
But if interface hello were to be a Top Level interface like below then only allowed modifiers are default or public.
interface hello {
}
Originally posted by Paul Salerno:

I still dont understand what is meant by this. If anyone has a code example, I would love to see it.
TIA
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Rajinder,
an "access modifier" is one of : public, private, protected, or none (default).
So when I say "any access modifier", that's what I mean.
There are other modifiers that can be used with different declarations, but they are not access modifiers.

interface MyInterface {} //ok
public interface MyInterface {} //also ok
private interface MyInterface {} //not ok, can't do this with a top-level class
protected interface MyInterface {} //also not ok.
class foo {
public interface MyInterface1 {} //ok
interface MyInterface2 {} //ok
private interface MyInterface3 {} //ok, it's nested
protected interface MyInterface4 {} //also ok
}
Rob
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Rob, thanks for clarifying up this point
I need to pay attention to those definitions more carefully!
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Rob Ross:
an "access modifier" is one of : public, private, protected, or none (default).
So when I say "any access modifier", that's what I mean.
There are other modifiers that can be used with different declarations, but they are not access modifiers.

interface MyInterface {} //ok (RK: --TRUE)
public interface MyInterface {} //also ok(RK: --TRUE)
private interface MyInterface {} //not ok, can't do this with a top-level class (RK: --TRUE)

protected interface MyInterface {} //also not ok.(RK: --TRUE)

class foo {
public interface MyInterface1 {} //ok (RK: --TRUE)
interface MyInterface2 {} //ok (RK: --TRUE)

private interface MyInterface3 {} //ok, it's nested (RK: --True)

protected interface MyInterface4 {} //also ok (RK: -- TRUE)

}
Hi all .. plz go through this
I do not know when discussion goes in wrong (edited NOTHING is wrong, I was wrong)direction then why not any BARTENDER intervein.(a humble request to bartenders) (edited THANKS Julinu)
HTH
[ January 24, 2002: Message edited by: ravish kumar ]

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Ravish, I suggest you actually try compiling the examples before you go and make yourself look foolish by making incorrect statements about correct code.
Rob
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
This code is from JLS (yes I did not compile it):

and can you plz tell .. how do you use inner Inertface.
And if you can provide one example in which any method of inner class is being used then I will be really greatful to you.
TIA
[ January 24, 2002: Message edited by: ravish kumar ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371

compile it and RUN it.
and Thanks for ur comments
[ January 24, 2002: Message edited by: ravish kumar ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Ravish, what do these two examples have to do with the original question of this thread?
The original question was "Can the interface be private and protected "
and the answer was
no for a top-level interface, they can only be public or default
YES for a nested interface, they can have any access specifier.
I supplied an example to show the point, then you said it was wrong, then you post some examples that have nothing to do with the original question.

Rob
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
agreed !!!
that compiles ... but JLS says NO
"interfaces are never inner"
Now let us come to the point ...
What we should answer if question comes?
I gave you above code .. which proves that u can not run such programs....
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well you are still answering a different question...and since I don't know what question it is you are answering, I can't really comment on your answer.
I never said anything about inner classes, or inner interfaces. I stated top level interfaces can only be marked public, or with default access, and that nested interfaces can be marked as private, protected, public, or default (no access specifier). Are you agreeing with me now, or disagreeing?
Rob
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi
first of all .. do not take thing in wrong way ..
Atleast I am here to correct my self and not to prove some one wrong ....
hahahaahahahhahahaah
did you try to compile and try to run my code....
I forgot to put [] after str in main() .. and it was not running .. i thought that it is not running bcoz of nested interface...
Now I just modified it and it is running perfectly fine
AW ... now tell me if you can ...
Why JLS says so
"interfaces are never inner"
This is the example from JLS ... again .. now no more use of code block
class HasStatic{
static int j = 100;
}
class Outer{
class Inner extends HasStatic{
static final x = 3;// ok - compile-time constant
static int y = 4; // compile-time error, an inner class
}
static class NestedButNotInner{
static int z = 5; // ok, not an inner class
}
interface NeverInner{}// interfaces are never inner
}
can you tell me why // interfaces are never inner
TIA
[ January 24, 2002: Message edited by: ravish kumar ]
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
Originally posted by ravish kumar:

can you tell me why // interfaces are never inner

Ravish,
I guess you have misunderstood what the JLS says. The example from JLS is from the 8.1.2 Inner Classes and Enclosing Instances topic.
What the JLS says there is that Inner classes cant have static members..
Since interfaces are implicitly static, you cant have an interface declaration inside an inner class.

HTH


Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Thanks ..
now I think .. as member(edited) interfaces are imlicitly static .. they are always nested... So interface are never inner
though they are nested ...
and as they are static they can not be nested in a inner class.
CMIW
TIA
[ January 24, 2002: Message edited by: ravish kumar ]
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
i really don't know what does "implicitly static" mean ?
but interfaces need not necessarily be nested.
You can interface like any other normal Top level class.
so can someone tell as to what does 'implicitly static" mean when referring to an Interface ?
Originally posted by ravish kumar:
Thanks ..
now I think .. as interfaces are imlicitly static .. they are always nested... So interface are never inner
though they are nested ...
and as they are static they can not be nested in a inner class.
CMIW
TIA
[ January 24, 2002: Message edited by: ravish kumar ]
[ January 24, 2002: Message edited by: ravish kumar ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi
plz chk this link from JLS
and CMIW
here it says
Member interfaces are always implicitly static.
above message need a modification, I will do it right now..
interfaces are imlicitly static -- wrong
Member interfaces are always implicitly static -- RIGHT
again CMIW
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces