• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding protected access member

 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,

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

Best Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic