Forums Register Login

package directory

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
>>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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote:The default moderator (package private)


You mean modifier instead of moderator, right?
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1515 times.
Similar Threads
Cant think of one:-)
Packages
Package visibility
Marcus Green - Exam 2 - Question 9
Reg. Packages
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:21:51.