This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Regarding protected access member
 
Similar Threads
Question on Overriding
NullPointerException
Private variable accessibity..?
Default constructor
Can anyone explain why the following doesnt compile...