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 - public and abstract 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 - public and abstract" Watch "Interfaces - public and abstract" New topic
Author

Interfaces - public and abstract

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Just a curiosity.
Interfaces are public and abstract, always.
Naturally, this code compiles fine.


However, this code doesn't compile.


So, if interfaces are always public, why just doesn't let the seconde case being legal?


Leandro Melo
SCJP 1.4, SCWCD 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[LM]: Interfaces are public and abstract, always.

Interfaces are always abstract, but they're not necessarily public. You may have heard that the members of an interface (the fields, methods, and nested classes (or interfaces) declared directly inside the interface) are always public. That's true.* But that's not the same as saying that the interface itself is public.

* Note I said, declared directly inside an interface. You could have a nested class inside an interface, and that class could declare a private field - but that field would not be directly inside the interface, and it would not be a member of the interface. In case you were wondering...
[ February 22, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Interesting. I thought I read in K & B's book that interfaces are always public (I'm pretty sure about that, at least in the portugues version of the book).
So they can have default access too, unh.
Thanks Jim.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
So, if interfaces are always public, why just doesn't let the seconde case being legal?


You can't have two public classes and/or interfaces in one compilation unit (source code file).

This version compiles OK:

[ February 22, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

You can't have two public classes and/or interfaces


...or enum or annotation (@interface)...


Tony Morris
Java Q&A (FAQ, Trivia)
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
That`s not exactly the point.
As I said, i thought i had seen in Kathy`s book (and i actually comfirmed it in the portuguese version of the book) that interfaces are always public.
So even if i didn`t insert the public keyword before interfaces it would be a public one, but this is not true.
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
hai i tried to put the interface inside a inner class but it give a compile error.



I am confused.

As Jim said an interface inside a class is abstract by default.
But when i tried to put the interface inside a inner class i get a compile error saying that " static declaration inside an non-static inner class".

So according to this compile error,the interface inside an inner class is static by default.

So is it like this,only static inner class can have interface as its member....???

Please explain
[ February 23, 2005: Message edited by: ramya jp ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
So is it like this,only static inner class can have interface as its member?

Actually, only an outer class, a static inner class, or an inner interface can have an interface as its member.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
As I said, I thought I had seen in Kathy`s book (and I actually comfirmed it in the Portuguese version of the book) that interfaces are always public.

K&B page 115 says:
"The public modifier is required if you want the interface to have public rather than default access."

Perhaps you were confused by this line:
"All interface methods are implicitly public and abstract."
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi Mike.
Now I understand that, but I have the portuguese version of the book.
I carefully took a look at it and it says:
"Interfaces s�o sempre publicas e abstratas ..."
Translating to english it means that interfaces are always public and abstract.
This for sure a translation error from the original version, just like some other ones that I`ve seen. It`s said, but the portuguese version of the book has quite a large number of errors for this kind of book.
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Thanks Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces - public and abstract