Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Class and Instance Methods

Whats the difference between class methods and Instance methods. Why do we need them separately? Can somebody please explain?

Class and Instance Methods

• Instances respond to instance methods

 - (id)init;
 - (float)height;
 - (void)walk;

• Classes respond to class methods

 + (id)alloc;
 + (id)person;
 + (Person *)sharedPerson;

Taimur

like image 337
Taimur Ajmal Avatar asked Jul 14 '10 10:07

Taimur Ajmal


People also ask

What is the difference between a class method and an instance method java?

Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference.

What is the difference between class method and instance method in Ruby?

In Ruby, a method provides functionality to an Object. A class method provides functionality to a class itself, while an instance method provides functionality to one instance of a class. We cannot call an instance method on the class itself, and we cannot directly call a class method on an instance.

What is difference between a method and an instance?

Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in which the method is defined.

What are the differences between static and instance methods in a class?

Static method means which will exist as a single copy for a class. But instance methods exist as multiple copies depending on the number of instances created for that class.


2 Answers

An instance method is only available on an instance of the class, while a class method does not need an instance but is available on the class.

Class Methods are denoted by a + while instance methods are denoted by a - before their return type.

Let's take NSObject for example. NSObject has a class method named + (id)alloc. The alloc method is used to allocate an instance of the class. Obviously alloc must be a class method because if it was an instance method, where would you get the "root" instance from?

On the other hand - (id)init is an instance method because it initializes the state of an instance.

like image 155
Johannes Rudolph Avatar answered Sep 20 '22 20:09

Johannes Rudolph


An example:

Human -> Class You -> Instance

Human could extinguish, you cannot. You could drink a Coke, Human cannot.

Instance method is only applied to individuals,

While Class method is applied to the whole group with the same identifiable features.

It's the difference between one and many, individual and the whole society.

[SomeClass alloc] means a new instance of the class is born just like You are given birth,

init applies to an Instance, like your parents give you a name, feed you and send you to school, so you have skills to live in this society.

like image 30
shader Avatar answered Sep 18 '22 20:09

shader