Results 1 to 9 of 9
  1. #1
    Mekie is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Override class method

    Hello

    I have a external jar library that has many classes and methods. My question is, how can I rewrite a existing method without changing the library?

    Ex:

    (External jar)
    Class Original{
    public int doit(a){
    return a+1;
    }
    }


    So normally using
    Original object = new Original();
    System.out.print(object(2));

    The output is 3.


    Now i want to alter the method to
    public int doit(a){
    return a+2;
    }


    I want the output be 4.

    Now, i cannot alter the Original class or any the coded used for object creation so creating an extension of class Original is not viable.

    Any thoughts?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Create a new class that extends the Original class and give it an override for the doIt method that does what you want. Is that understandable? If not, please ask. Also show us what you've tried and any error messages you might receive.

    Luck!

  3. #3
    Mekie is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Tried your tip, no success

    Main.java
    Java Code:
    public class Main {
    	public static void main(String[] args) throws Exception {
    		Original object = new Original();
    		System.out.print(object.doit());
    	}
    }
    Original.java
    Java Code:
    public class Original {
    	public int doit(){
    		return 2;
    	}
    }
    Mutated.java
    Java Code:
    public class Mutated extends Original{
    	@Override public int doit(){
    		return 3;
    	}
    }
    Output: 2
    Output expected: 3

    So, any advices on this? I can only change Mutated.java
    Last edited by Mekie; 10-31-2010 at 09:15 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm confused. Does the doit method take an int parameter? You know your doit code in your first post won't compile as the method signature shows it taking a parameter a, but never declares what the parameter is.

    Also, in your new main method, do you create a Mutated object or an Original object? This will only work if you use the new class.

  5. #5
    Mekie is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Also, in your new main method, do you create a Mutated object or an Original object? This will only work if you use the new class.
    Exactly my question. This is just an example, methods constructors doens't matter. I'm just allowed to change Mutated.java . Is there a way to do it?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Mekie View Post
    Exactly my question. This is just an example, methods constructors doens't matter. I'm just allowed to change Mutated.java . Is there a way to do it?
    Yes, just as you've done it above. It should work as long (again) as your main method uses a Mutated object, not an Original object. If it doesn't work, post all the code and the expected and observed output.

  7. #7
    Mekie is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    And if I'm not allowed to change the Main method? Any way of doing it?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Mekie View Post
    And if I'm not allowed to change the Main method? Any way of doing it?
    No. Your Main class has a main method that uses the Original class which can't be changed. So if nothing that is run can be changed, output can't possibly change.

  9. #9
    Mekie is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2010, 04:57 AM
  2. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  3. Override Entity Callback Method
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-13-2009, 06:05 AM
  4. Override/Extend Eclipse Class Loading
    By arun.ranganathan in forum Eclipse
    Replies: 0
    Last Post: 07-15-2009, 10:57 AM
  5. How to override Equals method with a Java File
    By JordashTalon in forum New To Java
    Replies: 6
    Last Post: 01-22-2009, 03:25 AM

Posting Permissions

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