Search code examples
javaobjective-c-category

Can Java do something like category in Objective C?


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.


Solution

  • 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.