extending String

J

jk

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
 
R

Ryan Stewart

jk said:
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.
[...]
 
T

Tony Morris

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?
 
A

Anthony Borla

jk said:
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
 
T

TheOne

The declaration of string class is "public final class String". You can
not extend it. Forget using in the same way.
 
B

Boudewijn Dijkstra

jk said:
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?
 
S

sanjay manohar

Why on earth exactly would you need this abhorrent syntax

"abc".method()

???
 
E

Eric Sosman

sanjay said:
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 ... */
 
A

Antti S. Brax

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.
 
Joined
Jul 31, 2008
Messages
2
Reaction score
0
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?
 
Joined
Aug 5, 2008
Messages
3
Reaction score
0
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. :-(
 
Joined
Jul 31, 2008
Messages
2
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top