File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes default package access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "default package access " Watch "default package access " New topic
Author

default package access

sathi jogi
Greenhorn

Joined: Aug 31, 2007
Posts: 14
// 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

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31

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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default package access