Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question - Philip Heller's book

 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book states :
" .... This "freebie" constructor is called the default constructor. It has public access if the class is public; otherwise its access mode is default "
few pages later it says: (chapter summary )
" ... This is the default constructor; it takes no arguments and is of public accessibility. "
My question is: which one is correct ? Is the default constructor always public like the second sentence makes me believe ?
Thanks
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a constructor is given no access modifier, then its access modifier is implicitly that of whatever the class has, ie public, protected or private. If the class has no access modifier, ie has default package access, then so does the constructor.
The only access modifiers which a constructor can optionally be declared with are public, protected or private.
Note that you can mix them, eg a public class can have a private constructor, though you won't be able to instantiate the class from outside.
[ May 04, 2003: Message edited by: Roger Chung-Wee ]
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not specify any constructors then Java automatically adds an invisible default constructor, from the moment you specify your own constructors with parameters Java will not add a default constructor.
The default constructor gets the same access modifier as the class access modifier.
If the class has public visibility, the default constructor has public visibility.
If the class has protected visibility, the default constructor has protected visibility.
If the class has private visibility, the default constructor has private visibility.
If the class has default visibility, the default constructor has default visibility.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rules specified in my reply above are intuitive.
This doesn't mean that when the constructor is accesible when the class is accessible !!!
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger and Thomas ,
I am little bit confused , with your responses.
I been thinking a class can only have default or public access.This is what I think I read and understood from Kathy/Bert book.
I know a constructor can have public,protected,private and default access.
Could someone please clarify.
Archana
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
package level class will have default and public, but enclosed classes can have public, protected, private, abstract, static, final.
like
class outer{
class inner{ // here a class can be declared with any of the access specifiers
}
}

badri
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A top level class can only have the public access modifer. A nested member class can have one of the acccess modifiers (public, protected or private) and can be static (does not apply to inner classes which by definition are non-static). Of course, they don't have to have any modifiers, in which case they will have default package access.
All can have the other modifiers (abstract, final and strictfp). Note that using both abstract and final makes no sense, so it is a compiler error. Furthermore, where more than one modifier is used, the recommended order of declaration is public, protected, private, abstract, static, final, strictfp.
Here are some examples.

[ May 05, 2003: Message edited by: Roger Chung-Wee ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Chung-Wee:
A top level class can only have the public access modifer.
A top level class can be public or default.
In fact, since you can have as many classes as you like in a single file (not nested) and since the file must be named after the one and only public class in the file, this rule is required.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic