File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extending Arrays class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extending Arrays class" Watch "Extending Arrays class" New topic
Author

Extending Arrays class

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Arrays class in util package is marked public.....

But when i tried to extend it .....a following error is thrown by my Compiler...

Arrays() has private access in java.util.Arrays


Thanks !!!

SCJP 6.0 96%

(Connecting the Dots ....)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Arrays is a utility class with only static methods which obviously can't be overridden. Why would you want to extend it?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Kapoor wrote:Arrays class in util package is marked public.....

But when i tried to extend it .....a following error is thrown by my Compiler...
Arrays() has private access in java.util.Arrays
Thanks !!!


Arrays has private Constructor?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Abimaran Kugathasan wrote:
Arrays has private Constructor?


Which can be quickly confirmed by taking a look at the source...

http://www.docjar.com/html/api/java/util/Arrays.java.html

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
IMO private constructor has nothing to take with subclassing.

Arrays is marked public so it should be subclassed even if all its members are static. I have an idea that static members cannot be overriden but can be redefined.

But my question is ....Arrays is marked public and still cannot be subclassed ??? Why ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Sahil Kapoor wrote:IMO private constructor has nothing to take with subclassing.


Of course the constructor affects subclassing. Remember that the constructor has to call the super classes' constructor via a call to super(). Even if no such call is made, a call to the default no-arg constructor is inserted. If the subclass doesn't have permission to access the super constructor, then it can't construct the super portion of the object ... hence, compile error.

Sahil Kapoor wrote:
But my question is ....Arrays is marked public and still cannot be subclassed ??? Why ??


The Arrays class *can* be subclassed ... but only from classes that can access the Array constructors. And since they are private, this is limited to the Arrays classes' inner classes.

Henry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Henry for that link....
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Thanks Henry Wong and Abimaran Kugathasan !!!

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

All the credits to Henry..... Thanks......
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Extending Arrays class
 
Similar Threads
list of File in a directory
Equality
Java Interview Questions
Arrays question
Arrays.sort