Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Override class method

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Override class method

    Main.java
    public class Main {
    	public static void main(String[] args) throws Exception {
    		Original object = new Original();
    		System.out.print(object.doit());
    	}
    }

    Original.java
    public class Original {
    	public int doit(){
    		return 2;
    	}
    }

    Mutated.java
    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

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Override class method

    Please do not double post your question. Your other post has been deleted.

    In the main, create the Mutated class, not the original class to expect an output of 3.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Override class method

    I'm just allowed to change Mutated.java . Is there a way to do it?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Override class method

    Quote Originally Posted by mekie View Post
    I'm just allowed to change Mutated.java . Is there a way to do it?
    You did change it, at least relative to Original.

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  3. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM