Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending Arrays class

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for that link....
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong and Abimaran Kugathasan !!!

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the credits to Henry..... Thanks......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic