Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend java.lang.String

java.lang.String is declared as final, however are there any mechanisms available legitimate or otherwise to extend it and replace the equals(String other) method?

like image 365
Darran L Avatar asked Feb 23 '12 18:02

Darran L


People also ask

Can we extend Java Lang String?

2) Strings are immutable and final in Java Since String is final there is no way anyone can extend String or override any of String functionality. Now if you are puzzled why String is immutable or final in Java.

What is Java Lang String?

The java.lang.String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.Strings are constant, their values cannot be changed after they are created.

Is Java Lang String can be subclassed?

String is immutable (More details of this concept are in the document). It does not allow the existence of subclass.


1 Answers

No, absolutely not. If you want some "other" kind of string, create another type which might contain a string:

public final class OtherString {     private final String underlyingString;      public OtherString(String underlyingString) {         this.underlyingString = underlyingString;     }              // Override equals however you want here } 
like image 55
Jon Skeet Avatar answered Oct 04 '22 04:10

Jon Skeet