This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access modifiers problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access modifiers problem." Watch "Access modifiers problem." New topic
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: 11441
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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: 18914
    
  40


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

Joined: Feb 04, 2013
Posts: 7
Thanks, this makes sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access modifiers problem.