I need to create a class with exactly the same methods as java.lang.String
.
What is the best way to do this in Java?
I know that I can't extend String class as it is final
. I am not looking at solutions where I need to copy the source code of java.lang.String
. For example, assume that I need the functionality length()
within my custom class named MyString
, which has a corresponding 'myLength()` method.
What is the best way to implement myLength()
?
I am not looking at various algorithms to find out the length of a string but to reuse String
's length()
method. Now once I have MyString
class ready, I should be able to use it anywhere for my custom manipulations.
As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.
The string is immutable means that we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend it and destroy its immutability.
Going through the various answers here, and the askers updates and clarifications, it appears that what the asker wants is a class that looks, smells and sounds like a String, but is not.
That is they would like to be able to do:
MyString string = "String!!";
This cannot work, since java.lang.String
is a final class, and so every "String"
that the compiler produces will be a java.lang.String
object, since this is not a MyString
object they cannot be assigned to each other.
In weakly typed languages you would be able to create such a class, since if a class looks, smells and sounds like a duck, then to all intents and purposes, it is a duck. Java, however, is a strongly typed language, and a duck is only a duck if it happens to be from the Anatidae family of birds.
Since final
classes cannot be subclassed, create a new class that has a String
instance inside and operate on this object.
public class MyString {
private String s;
public MyString( String s ) {
setInternalString( s );
}
public int myLength() {
return getInternalString().length();
}
private void setInternalString( String s ) {
this.s = s;
}
private String getInternalString() {
return this.s == null ? "" : this.s;
}
}
From your question it sounds like the thing you are looking for is simple delegation:
class MyString {
String delegate; // The actual string you delegate to from your class
public MyString(String delegate) {
this.delegate = delegate; // Assign the string that backs your class
}
int length() {
return delegate.length(); // Delegate the method call to the string
}
// other methods that delegate to the string field
}
I assume you mean java.lang.String
. However, you can't replace String
with your own class. You can make another class with the same methods (by copying the source code and changing the package declaration), but you won't be able to pass instances of that class to methods that require a String
.
Which brings up an important question: why do you think you want to do this?
Proxy design pattern, I guess. It's a stupid question, though (not a reflection on the asker, but on the person that asked him it in a job interview). Possibly the person asking you didn't realise that String is final? Otherwise I can't think why they would even ask.
Make a new class (it will need a different package, of course, implement the same interface and add all the public methods from the String class (read the javadoc to make sure you got everything).
I have to assume this is homework.
You may use the refactoring: Replace inheritance with delegation, which basically is ( as shown in previous answers ) to create an instance variable of the desired type and implement all its methods in the new class and passing the message to them.
Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.
You'll notice this is a lot of typing, that's when a good IDE comes handy.
The following video shows how IntelliJ IDEA 9 does it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With