• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

default package access

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic