About Question enthuware.ocajp.i.v8.2.1439 :
Moderator: admin
-
- Posts: 17
- Joined: Thu Oct 01, 2015 4:06 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1439 :
Good day!
I have question about constructors in this exemple.
As we see, there is no constructors in AccessTest class, it means that it will be provided by compiler, which inserts it whith "default" access modificator, so it willn't bee accessible from another package, isn't?
I have question about constructors in this exemple.
As we see, there is no constructors in AccessTest class, it means that it will be provided by compiler, which inserts it whith "default" access modificator, so it willn't bee accessible from another package, isn't?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
No, that is not correct. The access modifier of the constructor provided by the compiler is same as the access modifier of the class. Since the class is declared public, the default constructor will also be public.RAZER-KIEV wrote:Good day!
I have question about constructors in this exemple.
As we see, there is no constructors in AccessTest class, it means that it will be provided by compiler, which inserts it whith "default" access modificator
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Hi!
After reading answer's explanation, I still don't understand very well why C is also accessible by "ref". If I can't use "ref.c()", why are you including C as accessible? Is it because ref is a ClassTest object, and by this, it "implicitly" has c in it, although I can't reference it from ClassTester?
After reading answer's explanation, I still don't understand very well why C is also accessible by "ref". If I can't use "ref.c()", why are you including C as accessible? Is it because ref is a ClassTest object, and by this, it "implicitly" has c in it, although I can't reference it from ClassTester?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
c() is not accessible. That is why option 3, which says "Only d() can be accessed by ref", is marked as the correct option.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Oh, I'm so sorry, you're right!! I have confused the answers and thought the correct one was option 1 and I spent half an hour writing examples in Eclipse trying to find why C was also accessible
-
- Posts: 1
- Joined: Fri Nov 03, 2017 5:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Hello, I don' t understand why protected int c(); is not accesible from another package if the class extends.
Greetens
Greetens
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
A subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
Check out the section of JLS mentioned in the explanation for more details.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Mar 15, 2019 4:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Hi, I have also stumbled about this question, tried the code piece and accepted the explanation as logical. But after that, I' ve just declared the method c() in a parent class as "protected static" and seen, that now the method can be referenced in a subclass also through the ref of type super class. That confuses me and I can not understand such behavior. Have I missed something important? Thanks.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
The concept of polymorphism is applicable to instance methods only. So, yes, protected works differently for static members.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Jul 26, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
seems that this rule is not in place for static members. is it right!?admin wrote: ↑Fri Nov 03, 2017 11:21 pmA subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Please read the post just above yours.docian wrote: ↑Fri Jul 26, 2019 7:21 amseems that this rule is not in place for static members. is it right!?admin wrote: ↑Fri Nov 03, 2017 11:21 pmA subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Jul 26, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
I've seen the answer. the question is: how it is working for static members and what's the rule?admin wrote: ↑Fri Jul 26, 2019 7:33 amPlease read the post just above yours.docian wrote: ↑Fri Jul 26, 2019 7:21 amseems that this rule is not in place for static members. is it right!?admin wrote: ↑Fri Nov 03, 2017 11:21 pmA subclass can access the member even from another package. The restriction is only when the subclass (in another package) tries to access the member using a reference that is declared to be of type super class. In other words, a subclass cannot access a super class reference's protected member.
Check out the section of JLS mentioned in the explanation for more details.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
Rule is that protected static members can be accessed from a subclass using superclass or subclass reference.
Calls to static members are bound at compile time, so, the rule about protected access to instance members does not apply to static members. No reference is required to access static members anyway. If you use a reference to call a static member, it is resolved at compile time to the static method as per the declared type of the reference.
Calls to static members are bound at compile time, so, the rule about protected access to instance members does not apply to static members. No reference is required to access static members anyway. If you use a reference to call a static member, it is resolved at compile time to the static method as per the declared type of the reference.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
I somehow cannot get my head around this sentence (I understand all the rest).protected members of a class are accessible outside the package only in subclasses of that class, and only when they are fields of objects that are being implemented by the code that is accessing them
They are accessible in subclasses of subclasses - why?
It seems that in this case (I suppose the code at the end of explanation is still inside the AccessTester class?) they are fields of object (type: SubAccessTester) which is implemented somewhere else (not by the code accessing them).
-
- Posts: 2
- Joined: Wed Mar 01, 2023 1:15 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
no way people: protected methods of a superclass are visible in their subclasses (classes extending them), regardless of the package where the subclasses are defined or the static or dynamic nature of the methods. Even though I knew it, I also tried in my local IDE and confirmed this obvious rule therefore, the answer 3 is wrong, the answer 1 is right.
Please fix this bug asap.
Thank you in advance
Please fix this bug asap.
Thank you in advance
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1439 :
This is not a bug. It is a bit complicated to grasp so please do read the explanation carefully (multiple times, if required). It is correct.
Option 3 is correct. Try compiling the given code exactly as given and verify yourself.
Option 3 is correct. Try compiling the given code exactly as given and verify yourself.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 89 guests