# (wacky?) protected behavior

**URL:** <https://www.ruby-forum.com/t/wacky-protected-behavior/81109>\
**Category:** Ruby\
**Created:** [January 3, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109 "2007-01-03T23:59:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 3, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109/1 "2007-01-03T23:59:45Z")

</div>

class Foo  
def store(o)  
o.dave=1  
end  
end  
class SubFoo \< Foo  
def initialize  
@dave=2  
end  
def store  
super(self)  
end  
protected  
attr\_accessor :dave  
end  
sf = SubFoo.new  
sf.store  
p sf

The above code executes and shows that sf.dave has been set to 1. I  
assumed it would not work because I thought Foo should not have access  
to protected members which were defined in its subclasses. Is this a  
bug? If not, can someone explain why this works? Thanks!

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 4, 2007, 1:21am UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109/2 "2007-01-04T01:21:16Z")

</div>

David W. wrote:

> ```
> super(self)
> 
> ```
> 
> to protected members which were defined in its subclasses. Is this a  
> bug? If not, can someone explain why this works? Thanks!

But youre calling store from the subclass.

T.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 4, 2007, 2:06am UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109/3 "2007-01-04T02:06:55Z")

</div>

Trans wrote:

> But youre calling store from the subclass.

I gather what you mean is that o.dave=1 is executed from the context of  
a SubFoo and not from the context of a Foo. I guess thats how Ruby works  
but it doesn’t seem intuitive to me because I’m not sure the equivalent  
code would work in other languages I’m familiar with - namely Java and  
C++.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 4, 2007, 2:37am UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109/4 "2007-01-04T02:37:28Z")

</div>

David W. wrote:

> Trans wrote:
> 
> > But youre calling store from the subclass.
> 
> I gather what you mean is that o.dave=1 is executed from the context of  
> a SubFoo and not from the context of a Foo. I guess thats how Ruby works  
> but it doesn’t seem intuitive to me because I’m not sure the equivalent  
> code would work in other languages I’m familiar with - namely Java and  
> C++.

Thats right. I can say about th eother languages buit try this:

class Foo  
def store(o)  
p self # who am I?  
o.dave=1  
end  
end  
class SubFoo \< Foo  
def initialize  
@dave=2  
end  
def store  
super(self)  
end  
protected  
attr\_accessor :dave  
end  
sf = SubFoo.new  
sf.store  
p sf

T.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/wacky-protected-behavior/81109/5 "2007-01-06T19:30:25Z")

</div>

On 1/3/07, David W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Trans wrote:
> 
> > But youre calling store from the subclass.
> 
> I gather what you mean is that o.dave=1 is executed from the context of  
> a SubFoo and not from the context of a Foo. I guess thats how Ruby works  
> but it doesn’t seem intuitive to me because I’m not sure the equivalent  
> code would work in other languages I’m familiar with - namely Java and  
> C++.

Ruby != Java && Ruby != C

Jacob F.
