When a subclass outside the package inherits a protected member, the member is essentially private inside the subclass, such that only subclass and its subclasses can access it........ but the private members are not accessed by the subclasses of the class, then how is the first statment right? Am i clear or does it look ambiguous?
Thank you. You are using the word "private" to mean one thing in the first half of the sentence, then something different and more specific in the second half. Private access means that member can only be accessed from inside its own class. [That does include inner classes.] So far, so good?
If you miss out the access modifier keyword, you get default access: access to members from within their own class, and any other classes in the same package. But you don't get access from subclasses outside the package. Am I still making sense?
People who have only programmed in Java have no difficulty with "protected," but people who have programmed for a long time and have used C can get confused, because the protected keyword in C means something different. What it means in Java is that access is available from within the same package, and from subclasses, in the same package and in different packages. So, if you have a class foo in package "B" which inherits a protected member from a superclass bar in package "A," then access to that member in package "B" is confined to the class foo which inherits it.
I think that is what you mean, and if I have interpreted what you meant to say, you are correct. :-) CR
What I am trying to say is that if there is class A in package 'a' and it has a protected member variable.
package a; class A { protected int a; }
I have one more class B which is in a differnt package called b and it extends class A in package a. The proected member of class A, does it become private member to class B when it is extended or it remains protected?
Now can a class C in package b can access it without subcalling it?
Try it. Far better to run a set of little classes with those access modifiers in; your compiler will give you the answer. That is the correct response.
The incorrect response is, "No."
If your member in the subclass B in package b is inherited from class A in packaage a and has protected access, then the only way you can get access to it from other classes in package b which don't extend class A is to have accessor (get) and mutator (set) methods in class A or B which class C can gain access to. If they are in class A, they would have to have public access, if in B, then any access other than private would work.
CR
Post by:autobot
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards