Results 1 to 4 of 4
  1. #1
    ferno is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

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

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Here is my incomplete code you should be able to continue it. Here is some hints: using composition you can store an instance of a string in your class. As you want that this class behave just like the string class you need to recreate all the string method in your new class. These methods then will be a wrapper and delegate all its functionality to the string class.

    Below I've wrap some methods for your example, the equals(), length() and to String(). Now you can continue with the rest.

    Java Code:
    public class StringWrap {
        private String message;
        
        public StringWrap(String message) {
            this.message = message;    
        }
    
        public boolean equals(Object anObject) {
            return this.message.equals(anObject);
        }
        
        public String toString() {
            return this.message.toString();
        }
        
        public int length() {
            return this.message.length();
        }
    
        //
        // TODO: implements others wrapper methods here!
        //
        
        //
        // Your new add methods
        //
        public void add(int pos, char c) {
            int length = this.message.length() + 1;
            
            for (int i = 0; i < length; i++) {
                //
            }
            
            // TODO: add some more code
        }
        
        public static void main(String[] args) {
            StringWrap wrap = new StringWrap("hello");
            wrap.add(5, 'x');
        }    
    }

  3. #3
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Instead of keeping a String in your class, i guess keeping a StringBuffer would be better for performance reasons.

  4. #4
    ferno is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default

    thanks wsaryada, that worked, but now i have another question:

    i'm coming from c++, and my question is if I can overload operators like +, and the accessor function ( [] ) in java?

    because one thing i really want to do is be able to create an AdvString, lets call it s1, and be able to use s1[2] to get the character at position 2 of the AdvString...

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2008, 02:15 AM
  2. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  3. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM
  4. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •