• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thought I understood how to use the modifier 'protected '  RSS feed

 
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code sample which does absolutely nothing useful am simply using it to test my understanding of certain aspects of Java. Here I'm looking at protected variables. In Java, protected means that the member can be accessed by any class in the same package and by subclasses even if they are in another package.
I'm finding this so so frustrating that this basic stuff is tripping me up I honestly feel like throwing the towel in

If you look at the last segment of code I've noted the error I'm getting back. So what am I doing wrong here...

I also didn't realise that if you add a class and it's 'public' it's got to be created in a separate file. Why is this?



 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In class AnotherClassInAnotherPkg, you can only access your own protected member variables. In your runMe() method, you are creating another instance of AnotherClass and then you try to access its protected member variable. That's not allowed.

This is allowed:

Fergus Flanagan wrote:I also didn't realise that if you add a class and it's 'public' it's got to be created in a separate file. Why is this?

The Java compiler expects that a source file has the same name (and the ".java" extension) as the public class or interface defined in the file. You can't have multiple top-level public classes and/or interfaces in one file, otherwise you wouldn't know what to call the file.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:In class AnotherClassInAnotherPkg, you can only access your own protected member variables. In your runMe() method, you are creating another instance of AnotherClass and then you try to access its protected member variable. That's not allowed.


The specific rule regarding this is ... https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

In order to use a reference to access a protected member of a super class, the calling code has to be "responsible for the implementation". Since the calling code is from the AnotherClassInAnotherPkg class, the reference must be of type that IS-A AnotherClassInAnotherPkg (or subclass) -- and of course, so is the object.

So, this would work...


Henry
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fergus Flanagan wrote:I'm finding this so so frustrating that this basic stuff is tripping me up I honestly feel like throwing the towel in

Don't do that. There are all sorts of things that will be frustrating when learning any language, and Java being one of the "older" OO languages arguably has more than its fair share of "foibles".

Only three levels of access are required for an OO language:
  • public - anyone can see.
  • protected - visible only inside a class that inherits the member from a superclass.
    This does NOT include, as Jesper and Henry explained, accessing a protected member of another object.
  • private - visible only inside the class that declares the member.

  • Everything else - including 'package' access - is simply for convenience. Indeed, "packages" are a Java concept, and may well be called something different in another language (if they even exist).

    So Java's definition of protected - which includes package access - is arguably a bit of a kludge (and still annoys some purists ). Either Oak (the forerunner to Java) or Java v1 did include a private protected access which was as defined above, but it was dropped long before most people ever saw it.
    It may also be worth mentioning that in both C# and C++ protected does not include 'package' access. But they also include other - arguably unnecessary - access qualifiers that cause equal confusion when you're starting out.

    HIH, and don't despair. It does get easier.

    Winston
     
    Bartender
    Posts: 1801
    28
    Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fergus Flanagan wrote:
    I'm finding this so so frustrating that this basic stuff is tripping me up I honestly feel like throwing the towel in

    I felt like I wanted to bang my head against the wall when I was first learning this stuff. I would spend hours entering code from every book I could get my hands on. I became convinced that I just wasn't smart enough to understand the concepts. I had played around with older procedural languages, but this OOP stuff was mind boggling.

    But I stayed with it. For many months. And it finally started to click.

    So I understand your frustration. But I encourage you to stick with it. It really does get easier. Eventually.

    It is a difficult road, but the view is worth the climb. Hang in there.
     
    Fergus Flanagan
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hmm think I'm going to have to go away and take another look at modifiers. Thanks for your reply.
     
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Most modifiers are nice and easy to understand; protected is the one exception. It is an exception to the buzzword about Java® being simple. That does not mean easy, but it means that you can remember the rules without having to look them up.
     
    Fergus Flanagan
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Most modifiers are nice and easy to understand; protected is the one exception. It is an exception to the buzzword about Java® being simple. That does not mean easy, but it means that you can remember the rules without having to look them up.


    Don't think I would ever call Java easy or simple. As a matter of fact it's no easier than assembly, plsql or sql, that's from my own experience.

    So what is a difficult programming language if Java is supposed to be easier?
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Java® is really easy. C is really easy. Assembler is really easy.

    Programming is really difficult.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Most modifiers are nice and easy to understand; protected is the one exception. It is an exception to the buzzword about Java® being simple. That does not mean easy, but it means that you can remember the rules without having to look them up.


    Campbell, I am not sure if I agree with this. Granted, the protected access level is more complex than private and public, but those are the extremes (ie. "only one class" and "all classes" can access). Protected is in the middle, and as such there are qualifiers (package where declared and sub-classes) -- so, it has to be less easy relatively.

    I will agree that the "responsible for the implementation" rule is confusing, but that is the only additional rule to understand.

    Henry
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fergus Flanagan wrote:Don't think I would ever call Java easy or simple. As a matter of fact it's no easier than assembly, plsql or sql, that's from my own experience.

    Well in theory, assembler is supposed to be really easy because you have such a limited instruction set; but the sheer scope of an instruction such as "branch/jump indirect" and lack of safety constraints sends punies like me running back to their C (or Java) compilers.

    So what is a difficult programming language if Java is supposed to be easier?

    For me, Smalltalk was really tough, and I suspect I'd have similar problems with any language that treats code as a first-class object; even though I've written many scripts that build and execute code on the fly.
    I also have problems with functional languages, which is why I'm still struggling a bit with the new stuff in version 8. I can see it's power when a solution is presented to me, but it's not yet a "natural" way of thinking, so I find it difficult to come up with them on my own.

    But I like it; and I think that's probably the important thing. I'm happy to be a beginner again if it'll help me understand it, even though I've been programming for nearly 40 years.

    Winston
     
    Fergus Flanagan
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Fergus Flanagan wrote:I'm finding this so so frustrating that this basic stuff is tripping me up I honestly feel like throwing the towel in

    Don't do that. There are all sorts of things that will be frustrating when learning any language, and Java being one of the "older" OO languages arguably has more than its fair share of "foibles".

    Only three levels of access are required for an OO language:
  • public - anyone can see.
  • protected - visible only inside a class that inherits the member from a superclass.
    This does NOT include, as Jesper and Henry explained, accessing a protected member of another object.
  • private - visible only inside the class that declares the member.

  • Everything else - including 'package' access - is simply for convenience. Indeed, "packages" are a Java concept, and may well be called something different in another language (if they even exist).

    So Java's definition of protected - which includes package access - is arguably a bit of a kludge (and still annoys some purists ). Either Oak (the forerunner to Java) or Java v1 did include a private protected access which was as defined above, but it was dropped long before most people ever saw it.
    It may also be worth mentioning that in both C# and C++ protected does not include 'package' access. But they also include other - arguably unnecessary - access qualifiers that cause equal confusion when you're starting out.

    HIH, and don't despair. It does get easier.

    Winston


    Thank you. It was a case of "Okay I think I understand the concept" but when you go and code you then realise "Oh, I'm not sure about this, I'm stuck"

    No intention of giving up. I'm beginning to realise you only really learn the language when you are doing a project and not when you are learning the theory as a beginner.
     
    Fergus Flanagan
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    J. Kevin Robbins wrote:
    Fergus Flanagan wrote:
    I'm finding this so so frustrating that this basic stuff is tripping me up I honestly feel like throwing the towel in

    I felt like I wanted to bang my head against the wall when I was first learning this stuff. I would spend hours entering code from every book I could get my hands on. I became convinced that I just wasn't smart enough to understand the concepts. I had played around with older procedural languages, but this OOP stuff was mind boggling.

    But I stayed with it. For many months. And it finally started to click.

    So I understand your frustration. But I encourage you to stick with it. It really does get easier. Eventually.

    It is a difficult road, but the view is worth the climb. Hang in there.


    Yea, it was a bad day. Those kind of days you question your ability and maybe think am I too dense for this?

    Nice to know I'm not the only one that has to plod
     
    Fergus Flanagan
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:Well in theory, assembler is supposed to be really easy because you have such a limited instruction set; but the sheer scope of an instruction such as "branch/jump indirect" and lack of safety constraints sends punies like me running back to their C (or Java) compilers.


    I think I know what you mean. In my limited exposure to assembly programming with an 8051 microcontroller, which incidentally was the most enjoyable experience ever in IT.
     
    Ranch Hand
    Posts: 242
    14
    Android Angular Framework AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Fegus, I enjoyed assembly language, too.

    Simple response on "protected" keyword. Same-and-sub. It can be seen in the same class, same package, and in subclasses. You can also think of these access modifiers as concentric sets.

    public { protected { 'package' { private } } }

    Others no doubt pointed out that package is the default and the no-keyword visibility. Private is walled off for in-class use. Package/default is also available to same-package classes. Protected is also available to subclasses (for using inherited functionality / members as facilities to the subclass). The broadest is public. Those are the methods you want some unknown/not-necessarily-expected caller to use.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!