Results 1 to 9 of 9
Thread: Override class method
- 10-31-2010, 08:38 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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?
-
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!
- 10-31-2010, 09:13 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Tried your tip, no success
Main.java
Original.javaJava Code:public class Main { public static void main(String[] args) throws Exception { Original object = new Original(); System.out.print(object.doit()); } }
Mutated.javaJava Code:public class Original { public int doit(){ return 2; } }
Output: 2Java Code:public class Mutated extends Original{ @Override public int doit(){ return 3; } }
Output expected: 3
So, any advices on this? I can only change Mutated.javaLast edited by Mekie; 10-31-2010 at 09:15 PM.
-
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.
- 10-31-2010, 09:34 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
-
- 10-31-2010, 09:38 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
And if I'm not allowed to change the Main method? Any way of doing it?
-
- 11-01-2010, 06:26 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
SkeletonMidlet is not abstract and does not override abstract method
By just_in_deed in forum CLDC and MIDPReplies: 3Last Post: 08-13-2010, 04:57 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
Override Entity Callback Method
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-13-2009, 06:05 AM -
Override/Extend Eclipse Class Loading
By arun.ranganathan in forum EclipseReplies: 0Last Post: 07-15-2009, 10:57 AM -
How to override Equals method with a Java File
By JordashTalon in forum New To JavaReplies: 6Last Post: 01-22-2009, 03:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks