• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can the interface be private and protected ?
-Arsho
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Top-level interfaces can only have public or default access modifiers.
Nested interfaces (contained within another class) can have any access modifier.
Rob
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, can a nested interface be declared final?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, thanks for clarifying up this point
I need to pay attention to those definitions more carefully!
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compile it and RUN it.
and Thanks for ur comments
[ January 24, 2002: Message edited by: ravish kumar ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic