Take the 2-minute tour ×

Consider following situation:

Object > MyClass > MyClassA, MyClassB

If I want something in the Object level, for example, I added printDetail(); How can I do it in Java implementation? Moreover, can I override all the Object's method. For example, I need to have a whole new .toString(), can I override it? Thanks.

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

No it cannot, not really anyway. Objective C is a dynamically typed and scoped language, which makes it very amenable to features like categories. The closest you can come to this in Java is class instrumentation via a byte code manipulation library like ASM or Javassist.

But really, when using a strongly typed OO language like Java you should embrace its features rather than trying to duplicate those of another language.

share|improve this answer
great answer..!! –  newton_guima Jul 31 '13 at 18:02
add comment

All Java classes eventually have java.lang.Object Because of this, all Java classes inherit methods from Object. Half of these methods are final and cannot be overridden. However, the other methods in Object can be and are overridden
The toString() method in the Object class is used to display some information regarding any object.
So you can Override it according to your self...

public class Test{  
  public String toString() {  
  /// staff  
share|improve this answer
I see. I use ruby, that allow me to override the top level object. –  Ted Wong Mar 14 '12 at 7:22
add comment

As mentioned in the other answers, there is nothing really like a category. I do have some common solutions I use for some of the categories in my objective-c code when porting over to java. Many of my objective-c categories exist because I don't want to extend the iOS base classes but I do want to add some functionality to that class. Many of those objective-c categories do not add properties using objc_SetAssociatedObject. For those cases I use a static helper class in Java. Let's look at an example using NSString and String. I will add functionality to both to add quotes to the string. We'll assume this is useful and does not exist for the purposes of illustration. In objective-c we might have:

@interface NSString (MyCategory)

 * Creates and autoreleased image from self.
- (NSString*)quotedString;


@implementation NSString (MyCategory)

- (NSString *)quotedString
    return [NSString stringWithFormat:@"\"%@\"", self];


You would call this from somewhere like this:

NSString *myString = @"When you're curious, you find lots of interesting things to do.";
NSString *quotedString = [myString quotedString];

Here's how I would implement this in Java:

public class StringHelper {

    public static String quotedString(String that) {
        return '"' + that + '"';

And to call it:

String myString = = "When you're curious, you find lots of interesting things to do.";
String quotedString = StringHelper.quotedString(myString);

If you think of the category methods as methods that automatically send the self variable as the first method argument (albeit invisible) then this makes even more sense.

For your example, if I would not extend the specific object, I might do something like:

public class ObjectHelper {

    public static void printDetail(Object that) {
        // do what it takes;
share|improve this answer
add comment

Your Answer


