Can Java do something like category in Objective C?

DNB5brims picture DNB5brims · Mar 14, 2012 · Viewed 7.6k times · Source

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.

Answer

Perception picture Perception · Mar 14, 2012

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.