File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default package access

 
sathi jogi
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Note: there is no package name, so this class belongs to default package/project

public class Test

{

private Test()

{

System.out.println(� I�m in private constructor�)

}

protected Test(int i){

System.out.println(� I�m in protected constructor�)

}


Test(int i, int j){

System.out.println(� I�m in default constructor�)

}



public Test(int I, int j, int k)

{

System.out.println(� I�m in public constructor�)

}

}



Package mytest;

public class Test1 extends Test

{

public Test1()

{

Test publicTest = Test(10, 20, 30);

Test protectedTest = Test(10, 20);

Test defaultTest = Test(10);
}
}

Why it is not allowed extend a class which is defined in default package/project from the class which is in mytest package ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sathi jogi:
Why it is not allowed extend a class which is defined in default package/project from the class which is in mytest package ?


I don't think there is any such restriction, so you have probably done something wrong when you tried. Post your code.

However, use of the default package is a mistake, in anything other than a trivial one-class program. So maybe you should just ensure all your code is in a package.
 
MR Chen
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

However, use of the default package is a mistake, in anything other than a trivial one-class program. So maybe you should just ensure all your code is in a package.


So, exactly why is it a mistake?

If all the code is in one file, and all code in one file is considered to be in the same "default" package, then what's the difference between going with the default package and going through the trouble of defining your own package?

Is there an actual functional difference, or is it just considered "bad form" (i.e. the mark of a clueless n00b?)

Just wondering,
Mike
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know, the java.lang package is automatically imported. But what you might not realize is that the current package -- which might be the unnamed (default) package -- is also automatically imported. However, the unnamed package is not automatically imported if the current package is named.

So the problem with extending Test from your package mytest is that the unnamed package is not visible. And because it doesn't have a name, you can't import it.

To address MR Chen's question, this illustrates one reason for using packages. Another important consideration is in managing name spaces. (See this package section from Thinking in Java.)
[ September 05, 2007: Message edited by: marc weber ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic