|
EXTENDING the string class
ok, i know extending the string class is illegal because it's final, but i want to make an advanced string class that basically "extends" the string class and i've seen online this can be done through wrapper classes and/or composition, but i'm lost
here is my sample code that is coming up with numerous compile time errors due to the fact that when i declare a new AdvString object, it doesn't inherit the basic string features (note: Add is a method that can add a character to a specified location in a string)
class AdvString
{
private String s;
public AdvString(String s)
{
this.s = s;
}
public void Add(int pos, char ch)
{
int this_len = (this.length()) + 1;
int i;
for(i=0;i<(this_len);i++)
{
if(pos == i)
{
this = this + ch;
}
else if(pos < i)
{
this = this + this.charAt(i-1);
}
else
{
this = this + this.charAt(i);
}
}
}
public static void main(String[] args)
{
AdvString s1;
s1 = new AdvString("hello");
char c = 'x';
int i = 3;
s1.Add(i,c);
//s2 = Add(s1,i,c);
//String s2_reversed = Reverse(s2);
System.out.println("s1 is: " + s1);
}
}
any tips?
|