Can we override all the methods of String class in our class by extending String class?
6 Answers
I did not understand why you want to override String methods.
You can not do it as String class is final.
but you can add extra functionality to String methods by using composition like below
public class Example{
private String str;
public Example(String str){
this.str=str;
}
public int length(){
return str.length()+1;
}
Comments
You can't override final classes, of which String is one.
You can, though, create your own class with String helper functions.
e.g.,
public class StringHelper
{
public static String DoSomethingHelpfulToAString(String stringToDoStuffTo)
{
return "This string is now helpful";
}
}
then, just use the helper function on your string:
string stringToModify = "blah";
string modifiedString = StringHelper.DoSomethingHelpfulToAString(stringToModify);