The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Good question!!!

Posted by Kishori Sharan on December 11, 2000 at 10:33 AM

If you separate the two classes A and C in two different packages then it won't work. There is a reason for that. The class B is declared protected and its constructor is also protected. The meaning of protected memeber is that you can access it in only two cases
1) If you are accessing it from the same package.
2) If you are accessing it from its descendant.
I put A and C in two packages. Since C inherits A , C has access to the definition of protected B in class A. So in class C if you just write
B b ; // Note that I am just declaring a variable of type B
// I am not instantiating B
then its is OK because in class C you have access to the protected memeber of class A ( A being ancestor of C ) .
But you cannot access the protected construtor of B in class C because class B is neither ancestor of class C nor is it in the same package as class B ( A$B ) .
You can think of it differently as class A$B is a class whose construtor is protected. So you can always access its constructor from the same package or its descendant.
Thanks for asking such a good question.


  • one more... Christian Schmitt December 13, 2000 at 9:01 AM (1)
    • U got it... Kishori Sharan December 13, 2000 at 2:24 PM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us