This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem about protected attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem about protected attribute" Watch "Problem about protected attribute" New topic
Author

Problem about protected attribute

Moe Aptelia
Greenhorn

Joined: Apr 16, 2008
Posts: 3
Hi !
I have the following code:



Normally, the class Test3 shouldn't compile because a protected attribute should become private when it is inherited ! But actualaly, it compiles with Java5.0.

Does anyone could explain me why ?

Thanks in advance !
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
hi
i tried running case 3 and 4 and it failed as expected.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Moe Aptelia
Greenhorn

Joined: Apr 16, 2008
Posts: 3
Test4 shouldn't fail...
Test4 and Parent are in the same package. So the protected attribute x in Parent is visible in all the package package1...
anuj bhatnagar
Ranch Hand

Joined: Aug 06, 2007
Posts: 31
Superb example.
I'd like to add few things to this discussion.
If the Test3 class is packaged in a package other than package1 then you get the expected compiler error saying


but when the same package is specified as of the parent class it compiles fine.


thanks & regards,<br />Anuj Bhatnagar
 
wood burning stoves
 
subject: Problem about protected attribute
 
Similar Threads
Access protected member from the class of different package.
doubt in access modifier. Please explain this code
inheritance of protected attribute
Code from SCJP K&B Book won't compile
Rules for Protected and Default Members