| Start |
|---|
| Instance Variables and Class Variables Let's first brush up with what are the Instance Variables: They behave more like properties for an object. They are initialized on an object creation. Instance variables are accessible through instance methods. Per Object has per instance variables. Instance Variables are not shared between objects. Sequence class has @from, @to and @by as the instance variables. Class Variables Treat class variable same as static variables of java, which are shared among the various objects of that class. Class Variables are stored in heap memory. Shared among object and object1. Comparing the instance and class variables of Ruby against Java: |
| Access Controls Comparison of access controls of Java against Ruby: If method is declared private in Java, it can only be accessed by other methods within the same class. If a method is declared protected it can be accessed by other classes which exist within the same package as well as by subclasses of the class in a different package. When a method is public it is visible to everyone. In Java, access control visibility concept depends on where these classes lie's in the inheritance/package hierarchy. Whereas in Ruby, the inheritance hierarchy or the package/module don't fit. It's all about which object is the receiver of a method. For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver. This also means we can call a private method from within a class it is declared in as well as all subclasses of this class. Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class". Consider Public methods with maximum visibility Summary **Protected:** **Protected method** can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is "self" or "an object of the same class". |
Read Article
https://develop.horje.com/learn/1434/reference