Access Modifiers and Inheritance
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 4 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Fair point Campbell Ritchie,
looks a lot more concise.
noted for next time
looks a lot more concise.
noted for next time

Campbell Ritchie
Marshal
Posts: 81613
593
posted 4 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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:-
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.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.
Bobby Iveson
Greenhorn
Posts: 6
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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
posted 4 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 4 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 4 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Paul,
Thank you for the advise
Thank you for the advise
Campbell Ritchie
Marshal
Posts: 81613
593
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 4 years ago
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++.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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++.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Campbell Ritchie
Marshal
Posts: 81613
593
posted 4 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
But Java® changed its meaning.Tim Holloway wrote:. . . It's taken from C++. . . .
| 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 |










