Difference between subclass and category? [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between inheritance and Categories in Objective-c
What’s the difference and use of categories and inheritance?
thanks for your reply,for example we have nsstring class if we want to add methods to that class there is no need to create category for that,just we can subclass it but why we are using categories?Please help on this

Category adds some extra functionality to specific class (for example NSString). You don't need to declare the Object with that specific class name. You only import that category and all the Object implicitly become instance of the category, all the implementation is now available to them.
Where when subclassing, (sometimes you intently need to override the existing behavior/methods or you can add extra functionality too.) you explicitly declare that Object with the type like
MyCustomString *string;
and then all the methods become visible.

Related

What is an Object and a Class in python? [duplicate]

This question already has answers here:
What is the difference between objects and classes in Python
(5 answers)
Closed 3 years ago.
I came across the following line:
Every list object that you create in Python is actually an instance of List class.
What does Class and Object actually mean?
Another similar post
I saw the above post but in that they explain the difference between object and class. But what exactly is a class if Object is an instance of class.
Edit:
What is an instance?
Think of classes as blueprints for objects. So for example, you will only have one car model blueprint, and that is your class. Every single model that is produced is an instance. Hope it helps.

Abstract Methods/Classes in Objective-C? [duplicate]

This question already has answers here:
Creating an abstract class in Objective-C
(21 answers)
Closed 7 years ago.
I've realized there's no abstract classes in Objective-C. Is there any way to achieve the same functionality in some other way (a class that can't be instantiated, or methods that MUST be overridden by their subclass)?
It depends exactly what you want. If you would just use the class as an interface with no implementation then using a protocol instead is best. If you need some base or default implementation then you either document that the class shouldn't be instantiate do directly (common nowadays I think) or you implement the class to throw exceptions if it is instantiated directly (uncommon nowadays). Also throw exceptions in any methods that should be implemented by a subclass and are called on the superclass.
Unless you're creating a framework that will be delivered to other developers it's generally not worth the effort to implement, just document.

How to point to a child class during runtime but not compile time? [duplicate]

This question already has answers here:
What exactly is a so called "Class Cluster" in Objective-C?
(5 answers)
Objective C - How can I create an interface?
(4 answers)
Closed 7 years ago.
Problem: Suppose I have a class (call it cA), which has a reference to a helper class (call it HelperBase). The Helper class can be extended for specific implementations (call those HelperA, HelperB, etc.). However, I don't want cA to have any knowledge of the specific implementations either HelperA, HelperB, etc; I just want cA to call whatever methods are in Helper. Is there a way to do that? Is there a term for this? I'm also not sure what the terminology is for this kind of design (if there is one).
Examples in objective-c would be greatly appreciated. Thanks!

OOP : When should I subclass and when should I <include> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am wondering when I should subclass and I should include, let me explain you why :
I saw that whenever you include a class, you can access to it's methods, for example, if I create a new class that is a subclass of NSObject, I can still access to NSString and its methods just by including NSString.h
I also read that when you create a class that is a subclass of another class, you also access to it's methods.
So as a beginner to Objective-C, I just don't when should I use the first one and when should I use the second one ?
Subclassing is an object-oriented programming technique for creating a subtype of an existing type that inherits the supertype's behavior. #include is a compiler directive that substitutes the contents of the named file in its place. The two have very little to do with each other except that you have to include the file that declares a class in order to subclass it.
This question is kind of like "When should I build a doghouse and when should I use a hammer?"
Whenever there is a class that does what you want it to do, you can just use it. That's what you would do with NSString, for example.
When you need a new class that has nothing to do with any existing class, you subclass NSObject. Since NSObject is the subclass of everything in Objective-C, many people won't count that actually as "subclassing" but just as creating a new class.
When you need a class that is a modified version of an existing class, you subclass that class. However, Objective-C gives you some tools to avoid subclassing, and you shouldn't create a subclass unless you really have to. For example, you can add methods and even instance variables to existing classes without creating a subclass, and many classes are configurable through the use of delegate objects.

Why do we use interfaces instead of protocols? [duplicate]

This question already has answers here:
#interface and #protocol explanation?
(3 answers)
Class vs. Interface
(10 answers)
Closed 9 years ago.
I think that, except for the name, the protocols are much better suited to work as "interfaces" between classes. They do all that the #interfaces do (exposing properties and methods) and on top of that different classes can implement the same protocol which is a huge advantage in tandem with the dynamical nature of Objective-C. So why do we still use #interfaces? What advantages do they bring comparing with protocols? (I hope to get more out of this question than a "they are explicit in what they do" or "closed as not constructive".)
A delegate protocol needs to be defined as such
#protocol
//methods
#end
it can be put in any .h class, you just need to import i t whenever you are going to use it.
A protocol is not like a java interface, a protocol is an adapter that allows two classes to works together. Basically it says, if you want class A to send you messages about its state and actions these are the methods it will call on its delegate that you must implement. Its not like an interface because an interface says if you want to subclass this class you must implement these methods, the protocol says if you want to interact with this class you must implement these methods, so its somewhat different.
Objective C interfaces, delegates, and protocols