Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces - public and abstract

 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


...or enum or annotation (@interface)...
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic