extending String

Discussion in 'Java' started by jk, Feb 19, 2005.

  1. jk

    jk Guest

    Hello:

    I want to extend the String class. That in itself is not the problem.
    What I want to do is to be able to call my extension in the same manner
    that Strings can be called in Java. How can I do this.

    eg:
    ...
    "ABC".myMethod();
    ...

    class MyString extends String {
    ...
    public void myMethod() { ... }
    ...
    }

    Thanks in Advance
    John
    jk, Feb 19, 2005
    #1
    1. Advertising

  2. jk

    Ryan Stewart Guest

    "jk" <> wrote in message
    news:...
    > Hello:
    >
    > I want to extend the String class. That in itself is not the problem.

    Yes it is. String is final. This makes the rest of your post irrelevant.
    [...]
    Ryan Stewart, Feb 19, 2005
    #2
    1. Advertising

  3. jk

    Tony Morris Guest

    > I want to extend the String class. That in itself is not the problem.

    Yes it is - that is fundamental flaw to the remainder of your problem
    description.
    a) You can't.
    b) You shouldn't want to.

    Can you provide a proper requirements description instead of a solution that
    will never work?

    --
    Tony Morris
    http://xdweb.net/~dibblego/
    Tony Morris, Feb 19, 2005
    #3
  4. "jk" <> wrote in message
    news:...
    > Hello:
    >
    > I want to extend the String class. That in itself is not the
    > problem. What I want to do is to be able to call my
    > extension in the same manner that Strings can be called
    > in Java. How can I do this.
    >
    > eg:
    > ...
    > "ABC".myMethod();
    > ...
    >
    > class MyString extends String {
    > ...
    > public void myMethod() { ... }
    > ...
    > }
    >


    As other posters have indicated you can't create subclasses of
    'java.lang.String' [because it is a 'final class]. However, you *can* create
    a class having 'String'-like functionality, and can even use a
    'java.lang.String' object to do much of the work for you:

    public class MyStrClass
    {
    ...
    private java.lang.String s;
    ...
    // Use 's' to store your 'string data'
    public MyStrClass(String s) { this.s = s; }
    ...
    // ... various methods calling on 's' to perform tasks
    ...
    public MyStrClass append(MyStrClass msc)
    {
    ...
    }
    ...
    }

    This is an example of using composition, that is, building a new class by
    assembling together object(s) of other classes as members / attributes; you
    would:

    * Implement 'public' methods offering various client services

    * Use these internal objects to help you perform these
    advertised services

    The point to grasp is that you don't have to use inheritance to obtain
    extended functionality; rather, it is something best reserved for designs
    where subclassing is mandatory.

    Also, if it hasn't already occurred to you, if you override your new class's
    'toString' method, you will be able to use it in contexts, such as the
    following:

    MyStrClass mc = new MyStrClass("...");
    System.out.println("Your string is: " + mc);

    where a 'java.lang.String' object is expected, something you can do with any
    new class you create [since all classes descend from 'java.lang.Object',
    thus have a 'toString' method].

    I hope this helps.

    Anthony Borla
    Anthony Borla, Feb 19, 2005
    #4
  5. jk

    TheOne Guest

    The declaration of string class is "public final class String". You can
    not extend it. Forget using in the same way.
    TheOne, Feb 19, 2005
    #5
  6. jk wrote:
    > I want to extend the String class. That in itself is not the problem.


    Yes, it is. java.lang.String is final.

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 19, 2005
    #6
  7. "jk" <> schreef in bericht
    news:...
    > Hello:
    >
    > I want to extend the String class. That in itself is not the problem.
    > What I want to do is to be able to call my extension in the same manner
    > that Strings can be called in Java. How can I do this.


    How about implementing java.lang.CharSequence?
    Boudewijn Dijkstra, Feb 19, 2005
    #7
  8. jk

    dbr Guest

    dbr, Feb 20, 2005
    #8
  9. Why on earth exactly would you need this abhorrent syntax

    "abc".method()

    ???
    sanjay manohar, Feb 22, 2005
    #9
  10. jk

    Eric Sosman Guest

    sanjay manohar wrote:
    > Why on earth exactly would you need this abhorrent syntax
    >
    > "abc".method()


    int value_of_digit = "0123456789".indexOf(digit_as_char);

    boolean is_hello = "Hello".equals(string_ref_or_null);

    char array[] = "Examples abound".toCharArray();

    /* insert your examples here ... */

    --
    Eric Sosman, Feb 22, 2005
    #10
  11. wrote in comp.lang.java.programmer:
    > Why on earth exactly would you need this abhorrent syntax
    >
    > "abc".method()


    Let me guess: because that is the way method invocations are
    written in Java. It's a pretty fundamental part of any language,
    not just Java. HTH.

    --
    Antti S. Brax Rullalautailu pitää lapset poissa ladulta
    http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/
    "Disconnect this cable to shorten, re-connect to lengthen."
    -- Instructions on Logitech's USB mouse extension cord.
    Antti S. Brax, Feb 22, 2005
    #11
  12. jk

    Thor

    Joined:
    Jul 31, 2008
    Messages:
    2
    Location:
    SoCal
    Why would someone want to extend the String class?
    The same reason you would want to extend any other class. Sometimes you want an object to be a string and sometimes you want it to be something more. It is more simple to extend String than to call an additional method to convert back and forth.

    A better question is why is String final?

    At the very least, Java could have provided an interface that can be used instead of String.

    I'm working on a project where objects are passed around and reflection is used to operate on these objects. I need to pass a String object, that also needs additional functionality.

    We can discuss the merits of using such a system later. In the mean time, why can't I extend String (other than the obvious smart-ass answer: because it's final). Why is it final?
    Thor, Aug 1, 2008
    #12
  13. jk

    thusa12

    Joined:
    Aug 5, 2008
    Messages:
    3
    As you can guess, they have decided that everything that can be done with Strings are implemented in java.lang.String class.

    But you have come up with some set of requirements that needs to be added to String class.

    First thing to consider would be whether those new requirements are required for the generic String class or they are only specific to you.
    A wrapper solution would not help you since you can not pass a new classes instance as a String (since it's not a subclass of String).

    So I don't see there's a solution for what you ask. :-(
    thusa12, Aug 5, 2008
    #13
  14. jk

    Thor

    Joined:
    Jul 31, 2008
    Messages:
    2
    Location:
    SoCal
    Maybe in the next Java version they'll have the String class implement an interface. That would be helpful.

    I didn't start this thread. I'm obviously not the only one who wants to extend the String class.
    Thor, Aug 5, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Youngblood

    Extending string class

    Carl Youngblood, Jul 8, 2004, in forum: C++
    Replies:
    4
    Views:
    540
    Carl Youngblood
    Jul 10, 2004
  2. Jonathan Fine
    Replies:
    9
    Views:
    363
    Jonathan Fine
    Jun 23, 2007
  3. Rakesh
    Replies:
    10
    Views:
    12,174
    Mike Schilling
    Apr 8, 2008
  4. peter koch
    Replies:
    6
    Views:
    394
    James Kanze
    Mar 24, 2009
  5. James Kanze
    Replies:
    1
    Views:
    357
    James Kanze
    Mar 20, 2009
Loading...

Share This Page