Adding methods to exists class in android

Viewed 1679

I'm looking for a way to add some methods into exists class like this:

String s = "";
s.doSomething();

In objective C, I can use category to do this.

@interface NSString( Stuff)
-(void)doSomething();
@end

Is android has something like that? Or another hack?


Update: Actually, I got this problem: I use a class (not final) from jar file (so, I can't touch its source code). Then I want to add methods( or something like that) into this class without using inheritance. For example:

 public class Provider{
       // many methods and fields go here...
       public String getName(){}
 }

All I want to do is:

 provider.print(); //that call getName() method;

I also tried proxy pattern, it worked, but I don't like that way (because it like a wrapper class, I must store an object with many fields and methods to use only one method):

 public class ProxyProvider{
       Provider provider;
       public ProxyProvider(Provider provider){
           this.provider = provider;
       }
       public void print(){
           String name = provider.getName();
           //do something
       }
 }

Is there any way to solve that?

4 Answers
Related