aspose file tools*
The moose likes Java in General and the fly likes Extend String class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extend String class?" Watch "Extend String class?" New topic
Author

Extend String class?

Rahul Kaup
Greenhorn

Joined: Jun 16, 2009
Posts: 4
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Jun 16, 2009
Posts: 4
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

If you need to add functionality, perhaps you can create a utility class instead:
Instead of calling "mystring.hashCodeIgnoreCase()" you call "Strings.hashCodeIgnoreCase(mystring)".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul Kaup
Greenhorn

Joined: Jun 16, 2009
Posts: 4
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

Joined: Mar 08, 2014
Posts: 2
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

There's too much confusion, I can't get no relief.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
But welcome to the Ranch I presume you noticed it si a five‑year old thread?
John Muczynski
Greenhorn

Joined: Mar 08, 2014
Posts: 2
@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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend String class?