• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifiers problem.

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following method signatures from ArrayList:
boolean add(E e)
protected void removeRange(int fromIndexInclusive, int toIndexExclusive)

int size()and given:

import java.util.*;
public class MyUtil extends ArrayList{
public static void main(String[] s){
Myutil m=new MyUtil();
m.add("w"); m.add(""x); m.add("y"); m.add("z"); //#1
m.removeRange(1,3);
MyUtil m2=new MyUtil2().go();
}
}

class MyUtil2{
MyUtil go(){
MyUtil m2=new MyUtil();
m2.add("1"); m2.add("2"); m2.add("3"); //#2
m2.removeRange(1,2); //#3
return m2;
}
}

Shouldn't #1 and #3 cause compiler errors as add()method has default access and cannot be inherited? And also at #3, why does this cause compiler error?

This question is from Practic Exam book by Kethy and Bates 2011.(Page 88).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read our FAQ on how to UseCodeTags (<--that is a link). Unformatted code is hard to read. Many folks will see the unformatted source and decide it is not worth their valuable time to try and decipher what you wrote. Posting properly formatted code will most likely help you get an answer faster.
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma ishu wrote:Given the following method signatures from ArrayList:


and given:


Shouldn't #1 and #3 cause compiler errors as add()method has default access and cannot be inherited? And also at #3, why does this cause compiler error?

This question is from Practic Exam book by Kethy and Bates 2011.(Page 88).


The add() method of the ArrayList class is declared as public -- see JavaDoc ... http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add%28E%29

Henry
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you mean to say the question in the book is wrong?
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
sharma ishu wrote:Given the following method signatures from ArrayList:


and given:


Shouldn't #1 and #3 cause compiler errors as add()method has default access and cannot be inherited? And also at #3, why does this cause compiler error?

This question is from Practic Exam book by Kethy and Bates 2011.(Page 88).


The add() method of the ArrayList class is declared as public -- see JavaDoc ... http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add%28E%29

Henry


Please also explain me why does #3 cause compiler error?
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sharma ishu wrote:So, you mean to say the question in the book is wrong?


When in doubt, looking at the official documentation is a very good habit to get into.... in fact, the JavaDoc should always be a click away.



sharma ishu wrote:
Henry Wong wrote:
The add() method of the ArrayList class is declared as public -- see JavaDoc ... http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add%28E%29


Please also explain me why does #3 cause compiler error?


Well, what does the JavaDoc say?

Henry
 
Matt Baston
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, this makes sense.
 
Amit Ramesh
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello all,

My questions is regarding Line B above.
Why does this compile and run with no issue?
If a method is protected, I thought it can only be accessed by - 1) the dot operator if it is called from the same package or 2) via inheritance if one class extends the other.
MyUtil is not in the same package as ArrayList. So why can m.removeRange(1,3) be called in line B using the dot operator?
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ramesh wrote:
Why does this compile and run with no issue?
If a method is protected, I thought it can only be accessed by - 1) the dot operator if it is called from the same package or 2) via inheritance if one class extends the other.
MyUtil is not in the same package as ArrayList. So why can m.removeRange(1,3) be called in line B using the dot operator?


Well, does the other case apply? ie... is the MyUtil class a subclass of the ArrayList class?

Henry
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ramesh wrote:
If a method is protected, I thought it can only be accessed by - 1) the dot operator if it is called from the same package or 2) via inheritance if one class extends the other.
MyUtil is not in the same package as ArrayList. So why can m.removeRange(1,3) be called in line B using the dot operator?

Hi, Amit. You are correct that ArrayList::removeRange is declared protected. You are also correct that MyUtil is not in the same package as ArrayList. Yet line 6 should compile and run as expected. What other option does that leave you, and is that option true or false?

I am guessing that the "dot operator" is causing some confusion here. The expression object.method() is valid when method() is visible to the current scope. That includes when method() is declared public, or when it is is package-private and the current scope is in the current package, or when it is protected and the current scope is in current package or inside an inherited class definition, or when it is private and the scope is inside the defining class.

--Scott
 
Amit Ramesh
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott,

Thank you for this response. Does this mean that there is a difference between accessing a protected variable vs accessing a protected method from a subclass in a different package?

From Sierra Bates, I recall this example dealing with protected variables:



That example illustrates the idea that a protected instance variable cannot be accessed using "object.instanceVar" from a subclass, if that class is not in the same package.

So from your response, is it correct to conclude that if a subclass in not in the same package as it's parent, the protected methods CAN be accessed using object.method(), but the protected instance variable cannot?
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ramesh wrote:
Thank you for this response. Does this mean that there is a difference between accessing a protected variable vs accessing a protected method from a subclass in a different package?


No. The rules for accessing protected instance variables and protected methods of the superclass from the subclass are the same.

Amit Ramesh wrote:
That example illustrates the idea that a protected instance variable cannot be accessed using "object.instanceVar" from a subclass, if that class is not in the same package.

So from your response, is it correct to conclude that if a subclass in not in the same package as it's parent, the protected methods CAN be accessed using object.method(), but the protected instance variable cannot?


This is a different rule entirely. See section 6.6.2, of the JLS -- the subclass can only access protected superclass members that it is "responsible for the implementation".

https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

And this would apply for protected methods too. Try it, override a protected method, and then use a superclass reference to call the method -- that should also fail to compile.

Henry
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Amit. I am away from my development environment so I hope I am not steering you wrong. But it appears to me that the difference between your new example and the prior example is your line 11...

I believe if you replace that with...

... it will be comparable to the prior posting you wrote earlier today, and I believe line 12 will then compile and run.

It appears that the "dot operator" is clouding things a little in your understanding of Java syntax and scoping. Although it is essential that the dot be used in these examples, the dot isn't what is causing the error in your most recent example. It is the reference to a field that is not visible in the current scope. (Parent.x is not visible, but Child.x is.)

--Scott
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic