• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding protected access member

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also,

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

Best Regards,
 
reply
    Bookmark Topic Watch Topic
  • New Topic