• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extend String class?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the scene: I need to create a class MyString that should have all the methods of the java.lang.String class and a few new methods of my own.
Is there a way to get this done.

Now I know String is a final class so it cannot be extended. But.. "There must be some kind of way out of here, Said the joker to the thief"

Can anybody shed some light on this please.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is final... for good reason. There are a lot of optimizations happening behind the scenes for String objects (pooling etc.) that extending String could do great harm. Either way, if you are in need to extend String, you might want to rethink your design approach.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul,
Welcome to the Ranch.

Like you said, String is final. So you know the answer already.
I am very much curious, which functionality do you wish to add, which is lacking?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul:

Sorry, but the final keyword will prevent you from doing so. You could create a class that implements all of the interfaces and class methods of String, but that seems like a lot of work. Another option would be to create your MyString class as a wrapper class for String, with pass-through methods for String's class and interface methods. None of this actually extends String (in the language sense), but functionality-wise, this may solve your problem.

John.
 
Rahul Kaup
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.

Well I gave String as an example. I probably should have just said any final class.

John's answer is what I was looking for. Isnt this what we call composition?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite.

You can write your own implementation of a ResultSet by writing a new class that implements ResultSet, implements all it's methods and delegates all calls on that interface to the wrappered instance, or changes behavior.

The difference is that in the above example your implementation still is a ResultSet, hence all methods that take a ResultSet as an argument will also work with your implementation.

This is the traditional example of Composition which follows the 'Favor Composition over Inheritance' principle.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to add functionality, perhaps you can create a utility class instead:
Instead of calling "mystring.hashCodeIgnoreCase()" you call "Strings.hashCodeIgnoreCase(mystring)".
 
Rahul Kaup
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah that's a nice way to do it too.

Sebastian, I guess I gotta read some more to clear my concepts.

Well, thanks everybody. Appreciate it.

Cheers.

Rahul
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If someone is really making a class which is a string implementation, then you might consider implementing the CharSequence interface.
One example would be a rope implement of strings, for example http://ahmadsoft.org/ropes/doc/index.html
The CharSequence is generally converted to a String using the toString() method, so it is largely interoperable. You can add it with strings for example.
Perhaps you'd like to take a look at the StringBuilder class?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's too much confusion, I can't get no relief.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But welcome to the Ranch I presume you noticed it si a five‑year old thread?
 
John Muczynski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie
You're asking me if I realized it was a 5 year old thread? Or Stephan?
Yes, I did. That's why I started with "If someone is"

Lots of people like me do a search and get a page like this. So, I thought it would be helpful to post the answer that helped me.
I am needing to update a legacy class that basically represents the String name of something. So having it be more inter-operable with String would help the code.

Your Moose Saloon page about String is the top hit for the keywords I typed in ! Congratulations.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Muczynski wrote:Lots of people like me do a search and get a page like this. So, I thought it would be helpful to post the answer that helped me.
I am needing to update a legacy class that basically represents the String name of something. So having it be more inter-operable with String would help the code.


In which case I suspect you're right: CharSequence sounds ideal.

I was also interested in the "ropes" link. I ran into it a while back, and didn't give it much thought; but I'm now wondering if there might not be a much simpler implementation possible based on a SkipList (which are, by definition, self-balancing).

Such are the things that occupy geeks on Sunday afternoons.

Winston
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Muczynski wrote: . . .You're asking me if I realized it was a 5 year old thread? . . . Yes, I did. . . .
Your Moose Saloon page about String is the top hit for the keywords I typed in ! Congratulations.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic