Forums Register Login

Extending Arrays class

+Pie Number of slices to send: Send
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 !!!
+Pie Number of slices to send: Send
Arrays is a utility class with only static methods which obviously can't be overridden. Why would you want to extend it?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 ??
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Thanks Henry for that link....
+Pie Number of slices to send: Send
Thanks Henry Wong and Abimaran Kugathasan !!!

+Pie Number of slices to send: Send
All the credits to Henry..... Thanks......
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2756 times.
Similar Threads
Arrays.sort
Java Interview Questions
Equality
list of File in a directory
Arrays question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:21:15.