• 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

Kathy / Bert interface question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
i was reading the Kathy/Bert book (very good) , but I got some questions .
First : Every file can contain only one public class or interface .
Second : The book tells (at least I understood this way) that all interfaces are implicity public and abstract .
Question:
How can I have the following file ?
interface A
{
void doSomethingA();
}
interface B
{
void doSomethingB();
}
public class C
{
public static void main(String [] args)
{
//do really important thing (like the book
//exmaples :-) )
}
}
Wouldn't I have 3 public things in one file ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, interesting question. My next move to find out would be to compile your file.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting result. Try it also by explicitly declaring the interface A to be public.
 
Thor Silva
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case we get an error ... I would like to know if the book is correct in this point or if there is something in the JLS that makes this an "exception of the java laws" :-)
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All interfaces, both top-level and member, are implicitly abstract. A top-level interface declared without a modifier (default access) may be accessed only from within the package in which it is declared. So, a top-level interface must be declared with the public modifier to be publicly accessed.
Your original code is therefore perfectly valid, with two default modifiers and one public modifier. By making an interface public, you get an error because the compiler will only allow one public modifier per source file.
All member interfaces are implicitly public and static (and abstract, of course).
[ March 31, 2003: Message edited by: Roger Chung-Wee ]
 
Thor Silva
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roger... so let me see if I understood ...
Interfaces can have access modifiers public or the default (no access modifiers) . You wrote that all member intefaces are public and static ... in the Kathy / Bates Book , the member variables are public static and final implicity but the member methods are public and abstract (and not static because something abstract can't be static) . Is that right or i didn't understand something ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I found from a quick scan of the JLS it says "all members of interfaces are implicitly public", not "all interfaces are implicitly public". That you can also play around with some coding. Are there "package scope" interfaces?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface's members can comprise interfaces, classes, methods and fields which are declared in the interface or inherited from direct superinterfaces. This inheritance excludes interfaces, classes and fields that it hides and methods that it overrides.
As I said, interface members are implicitly public, static and abstract. If you look at the interface members in more detail, you can see what the various accesses are.
Member interface: public, abstract and static (all implicit) and strictfp. protected and private are also allowed, but only if declared in a top-level class.
Member classes: public, abstract and static (all implicit), final and strictfp.
Member methods: public and abstract (all implicit), no other modifiers are allowed.
Member fields: public, static and final (all implicit), no other modifiers are allowed.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like Roger has got it!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Roger, but I also just read these chapters and need a little bit of clarification. I guess I'm a little unclear as to what a "member" really is.
In the first part of your last message you said:

interface members are implicitly public, static and abstract.


However, later on you said:

Member methods: public and abstract (all implicit), no other modifiers are allowed.
Member fields: public, static and final (all implicit), no other modifiers are allowed.


So all members are implicitly abstract, and all member fields are implcitly static. And I know that a method can't be static and abstract at the same time. So how can a field be static and abstract at the same time? Or am I missing something? Or did you mean something else?
Thanks!
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that it is the JLS which lacks clarification, because the JLS says that interface members are implicitly public, static and abstract. But the only interface members which conform are interfaces and classes.
Member methods cannot be static because they are implicitly abstract, and you can't have an abstract method which is static.
Member fields are actually constants, are implicitly final and therefore cannot be abstract as well.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thor -
Roger's got it!
I think if you re-read page 115 you'll see the distinction between an interface (where 'default access' is implied), and interface methods (where 'public' is implied).
-Bert
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic