• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package access scope

 
william su
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think the answer is "Compilation error, in package trunk3 at line 4".Because the Account Constructor is default access.It only can be access in package trunk1.
But,i am wrong.
guys,who can tell me why?
Thanks a lot!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please note that if you copy a question from a book, mock exam or other source, we require that you QuoteYourSources - in other words, please tell us where you copied this question from.
 
william su
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,Jesper Young,I forgot it.

It comes from JavaChamp.

http://www.javachamp.com/questions/packages-and-access-scope-in-java.xhtml

Thanks a lot.
 
Abhishek Sawant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compilation error, in package trunk2 at line 2
Compilation error, in package trunk3 at line 4.
These 2 are correct answers...

Reason:
Constructor can be default. So option 3 is not wrong. Anything declared as default has access in that package only. Not even in the subclasses out of package(protected members have this property). In package trunk2, when you try to extend some class from package trunk1, compiler will complain as that class has default constructor. And as I said above, you cannot access anything that has default tag, outside the package, even if you extend the class from first package.

I hope this point is clear to you.

Now, a line in Tester file:
Account c = new Account();
This line is trying to do same as we discussed above. Accessing default members outside package. Compiler won't allow it.

If you still have any doubt, please ask for the same.

Regards,
Abhishek.
 
william su
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,thank you very much Abhishek Sawant!!

I have already understand.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic