• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extend String class?

 
Rahul Kaup
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
John Muczynski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's too much confusion, I can't get no relief.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic