| Author |
Access modifiers problem.
|
sharma ishu
Ranch Hand
Joined: Sep 10, 2012
Posts: 70
|
|
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
Joined: Oct 02, 2003
Posts: 9955
|
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
sharma ishu
Ranch Hand
Joined: Sep 10, 2012
Posts: 70
|
|
|
So, you mean to say the question in the book is wrong?
|
 |
sharma ishu
Ranch Hand
Joined: Sep 10, 2012
Posts: 70
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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:
Please also explain me why does #3 cause compiler error?
Well, what does the JavaDoc say?
Henry
|
 |
Matt Baston
Greenhorn
Joined: Feb 04, 2013
Posts: 7
|
|
|
Thanks, this makes sense.
|
 |
 |
|
|
subject: Access modifiers problem.
|
|
|