• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default (package) vs public

 
Eric Gonzalez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is teh different between default package and public?
thanks in advance
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default package has no name. All classes and interfaces that are not preceded by a package sentence, and that are in the same directory are placed in the same default package. That is, every directory has two packages the default an the package ends up with the same name as its directory.
However you should not use the default package except for testing purposes.
The classes that are in a named package must be placed in a directory path that reflects the package names. The parent directory of this directory structure must be placed in the classpath , otherwise the compiler/JVM won't find the classes in that package.
Now, are you asking about "public package"?
packages cannot have access modifiers, so in that sense all of them are public. The top level classes and interfaces in a package can be friendly or publicly accessed.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question might be what's the difference between public accessibility and default (a.k.a) package accessibility???
here's a quick list from A Programmer's Guide to Java Certification:
public - Accessible everywhere
protected - Accessible by any class in the same packae as its class, and accessible only by subclasses of its class in other packages
default(no modifier) - Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
private - Only accessible in its own class and not anywhere else.
 
Eric Gonzalez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right Jessica tha is what I meant, but the above reply was helpful too.
Is this correct?
least restrictive --------------->most restrictive
public,protected,default,private
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Eric
it's correct. default is between protected and private.
BTW, sometimes default access modifier is called as 'friendly'
Thanx,
Jamal Hasanov
www.j-think.com
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always remember the default (modifier) by stating that it provides package accessibility (see Mughal's Java Cert. book).
default (no modifier): Only accessible by classes, including subclasses, in the same package as its class (package accessibility).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic