Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I understand Objective-C categories and how they become useful, but I always have a hard time explaining the concept to other programmers that are not familiar with Objective C.

Maybe I'm just bad at explaining things, but I was thinking at another way to explain it by comparing to similar features offered by other (more popular) languages. (ex : I can explain the similarities between Objective C protocols and Java Interfaces)

Any examples similar to Categories ?

share|improve this question
2  
Side note: Objective-C (and not C++ as is often falsely reported) was the main influence on Java. So, the similarities between Objective-C protocols and Java interfaces are easy to explain: they are the same thing! –  Jörg W Mittag Oct 17 '12 at 16:28
    
Categories are not a difficult concept: A category adds methods to a class. –  Caleb Oct 17 '12 at 16:34
    
@JörgWMittag that's true. –  user4051 Oct 19 '12 at 9:58

2 Answers 2

up vote 4 down vote accepted

C#'s class extension methods are basically the same thing done safely: you can only define methods that don't already exist, you can't violate encapsulation, and you can't compile code that imports the same extension method twice from different static classes.

share|improve this answer

I dont have much experience with Objective C.

However, from what I've read, the Scala: Traits or Mixin Class Composition seems to be closest to categories. Alternatively its a special case of multiple inheritance perhaps (C++).

I dont think Java has anything quite like it.

share|improve this answer
    
Categories are not a special case of multiple inheritance. When you create a category on a class, you're changing the class itself, not creating a subclass. Likewise, categories are not class composition. It's true that categories can be used to add an implementation of some interface to an existing class, and MI and composition can also be used to implement interfaces, but that's as far as it goes. –  Caleb Oct 17 '12 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.