• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Access Modifiers and Inheritance

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm about to start training/preparing for my OCP and have just reviewed all of the oca material from SE8 OCA Programmer 1 book, which I used to prepare for the OCA.

There is one thing that keeps niggling away at me and that I still don't fully understand. It's to do with the behaviour of member access during inheritance.

Sorry, if I don't explain this very well:

if I have say 4 classes all related via inheritance but in different packages:



I really appreciate any feedback on this and thank you in advance for the time any of you may take to look at this

Bobby
 
Marshal
Posts: 81613
593
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the code would have looked like with the long comments changed to /* comments */ and the long lines broken:-
 
Bobby Iveson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair point Campbell Ritchie,

looks a lot more concise.

noted for next time
 
Campbell Ritchie
Marshal
Posts: 81613
593
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid the long comments inside the code tags are illegible. I think I shall have to edit your post. Please avoid slang terms like “cos”. Some readers here depend on Google Translate, and Google Translate can't translate cos correctly. Well, it seems to translate it into Spanish but not French or German!

Remember the following rules, which are really simple:-
  • 1: Public access: accessible anywhere in the CLASSPATH, which usually includes the current directory.
  • 2: No modifier=default access, also called package‑private. Accessible in the same package only.
  • The following rule is rather simple:-
  • 3: Private access: accessible in the same top-level class, which includes nested classes.
  • The following rule is harder to understand:-
  • 4: Protected access: in the same package and in code responsible for implementation of the object.
  • Look in the JLS (=Java® Language specification). Not in subclass (please don't say parent/child)=no protected access.

    If the access is by a simple class instance creation expression new C(...), or a qualified class instance creation expression E.new C(...), where E is a Primary expression, or a method reference expression C::new, where C is a ClassType, then the access is not permitted.

    I can never remember how protected access works, but I think the real reason you can't get gp.sayHi(); to compile is that you are not in code responsible for implementing an object. Your lines 15‑16 permit access because you are in the same class and all members are accessible anywhere in the same class, even with private access. But the gp reference in your line 18 isn't in code responsible for implementing the object. It is in a static method, which isn't part of any object at all. You will have to go through the JLS carefully because protected access is confusing.
     
    Bobby Iveson
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for your feedback Campbell,

    and I'll try not to use slang in the future...

    I will check out the JLS and see what I can find out.

    Wait? I'm a little confused, did I not refer to the class relationship as sub or super in my post or are you referring to the naming of my classes?

    if so, is there a reason for this?
     
    Campbell Ritchie
    Marshal
    Posts: 81613
    593
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's a pleasure
    The class names seem to suggest there is a similarity between biological inheritance and class inheritance; the two are different. Yes, it was obvious they were subtypes; I tried you code but changed the names to SuperType, SubType, etc.

    Remember that with Java9, the rules changed; access now also depends on whether different modules can access each other.
     
    Bobby Iveson
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No way!! That's interesting.

    I've never thought about the language changes that could come with the later versions of Java...

    I'm currently studying Java 8 as part of a traineeship I'm doing. I hope to start my very first position later in the year as a trainee developer.

    Do you think I should start to familiarise myself with one of the later versions, which of the later versions are most mainstream in the production environment? I know Java 11 is the latest.

     
    Marshal
    Posts: 28492
    113
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm running Java 15 here at home. But chances are, if you get a Java programming job you won't be using anything later than Java 11, and quite likely you'll be using Java 8.

    However, yes, if you have some spare time then looking at post-Java-8 features would be useful, for example it would broaden your mind regarding what's possible with Java. Just don't let that interfere with getting a firm grounding in Java 8.
     
    Bobby Iveson
    Greenhorn
    Posts: 6
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Paul,

    Thank you for the advise
     
    Campbell Ritchie
    Marshal
    Posts: 81613
    593
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As Paul C hinted, Java11 is the current long term version, but the biggest change since Java8 was modules and that happened in Java9. I agree with Paul: get well‑grounded in Java8 and then learn the enhancements.
     
    Saloon Keeper
    Posts: 29001
    214
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch
    I can never remember how protected access works...



    It's taken from C++. Protected access is private to the implementing class and its subclasses, whereas private access is private to the implementing class only.

    C++ also defined something called "friend" access, but that, fortunately was never adopted by Java. It's not even very useful in C++.
     
    Campbell Ritchie
    Marshal
    Posts: 81613
    593
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Holloway wrote:. . . It's taken from C++. . . .

    But Java® changed its meaning.
     
    Do Re Mi Fa So La Tiny Ad
    The new gardening playing cards kickstarter is now live!
    https://www.kickstarter.com/projects/paulwheaton/garden-cards
    reply
      Bookmark Topic Watch Topic
    • New Topic