File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding protected access member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding protected access member" Watch "Regarding protected access member" New topic
Author

Regarding protected access member

PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110


First please forgive me for the class names and the method names.
'My doubt is in class A the method "print" is declared protected .
So when class B inherits the Class A the method "print" becomes private in class B.
then when i execute B b=new B(); b.print(); in line 1 the sompiler must throw an error saying trying to access a private method.

But this code executed perfectly.

Please tell me what i have missed.


To err is human,
To forgive is not company policy
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

Please quote the sources of the code snippet above.

Why would print() become private in B. Class B inherits class A, so it will inherit the method.
Child classes inherit the protected methods of parent classes. If you separate the packages though,
then only way you can access the protected method is through inheritance.




Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Also,

Try to see the difference between the default access and protected access.

Best Regards,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding protected access member