Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package directory

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was going through following question from site
http://www.jchq.net/certkey/0102certkey.htm


Question 9)What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
afancymethod();
}

Answer 9)4) P1 compiles cleanly but P2 has an error at compile time

Even though P2 is in the same directory as P1, because P1 was declared with the package statement it is not visible from P2

I have not understood answer. does directory and package both refer same. please advise. thanks in advance
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P1 is in the MyPackage package, and has package-private access. P2 is in the unnamed package, and cannot see non-public classes in that package.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>and cannot see non-public classes in that package.

I have not understood clearly. Can you please elaborate.Can it see default scoped ones. please advise
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Lee wrote:>>and cannot see non-public classes in that package.

I have not understood clearly. Can you please elaborate.Can it see default scoped ones. please advise

The default moderator (package private) means only classes in the same package can access it. Since p2 is in a different package, it cannot.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The default moderator (package private)

You mean modifier instead of moderator, right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic