• 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

Please help me to get Java Certification

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I need help to understand why this code does not compile:








The error is "The method doThis() from the type A is not visible" ... why? It's protected and B extends A. I don't understand..
TKs
 
Sheriff
Posts: 7145
1364
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Tony, Welcome to JavaRanch.

According to JLS

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


That means, you cannot access doThis() method directly on a reference of A. Instead, you can call it through a reference of B as B is a subclass of A

 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a follow up question. I tried to duplicate the problem above with my own program. I created ChildClass that extends ParentClass with parent class having the protected parentMethod(). I then wrote a main.java as my driver which new ParentClass(). However, when I test it on my computer it did work on mine which I expected it to be . Can you explain the difference? I have my files in one folder. Or maybe I am not following the concept/question?

Regards,
John
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Miska wrote:
I have a follow up question. I tried to duplicate the problem above with my own program. I created ChildClass that extends ParentClass with parent class having the protected parentMethod(). I then wrote a main.java as my driver which new ParentClass(). However, when I test it on my computer it did work on mine which I expected it to be . Can you explain the difference? I have my files in one folder. Or maybe I am not following the concept/question?



From Devaka's quote of the JLS...

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.



The parent class and the child class are in different packages in this example.

Henry
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
Tony King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys! Now I understood the concept. Soon I will ask help for other questions!
bye

TK
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Feel free to. Welcome to javaranch
 
Devaka Cooray
Sheriff
Posts: 7145
1364
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Please Use Code Tags when you post a source code...


I wonder about the Meaningful Subject Line too
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:I wonder about the Meaningful Subject Line too


Yup, that too
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you Henry!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic