Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 283
DNB5brims Avatar asked Mar 14 '12 04:03

DNB5brims


People also ask

What is difference between category and extension?

Category and extension both are basically made to handle large code base, but category is a way to extend class API in multiple source files while extension is a way to add required methods outside the main interface file.

What is category in IOS?

You use categories to define additional methods of an existing class—even one whose source code is unavailable to you—without subclassing. You typically use a category to add methods to an existing class, such as one defined in the Cocoa frameworks.


1 Answers

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.

like image 129
Perception Avatar answered Sep 21 '22 15:09

Perception