Tuesday, February 14, 2012

Singleton class. Singleton method Vs Class method.

Every object is associated with two classes. One is which we can get using class method of object(eg: "hello".class) and the other is called the eigenclass(or singleton class).

The singleton methods of an object are the instance methods of the anonymous eigenclass associated with that object.

To open up an singleton class of an object str, use class << str

Eg:


 Thus, singleton class is an anonymous class associated with every object.


A method added to a single object rather than to a class of objects is known as singleton method. Also the class methods on a class are nothing more than singleton methods on the Class instance that represent that class.

Eg:


On the other hand, class methods are invoked only through the constant refering to the Class object.

Eg:

   

No comments:

Post a Comment