• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

default package access  RSS feed

 
Greenhorn
Posts: 14
  • 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 ?
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 1970
  • 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.
 
Ranch Hand
Posts: 32
  • 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
 
Sheriff
Posts: 11343
Java Mac Safari
  • 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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!