# Inheritance v passing object through the constructor

**URL:** <https://www.ruby-forum.com/t/inheritance-v-passing-object-through-the-constructor/142379>\
**Category:** Ruby\
**Created:** [July 7, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/inheritance-v-passing-object-through-the-constructor/142379 "2008-07-07T13:41:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 7, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/inheritance-v-passing-object-through-the-constructor/142379/1 "2008-07-07T13:41:05Z")

</div>

Hi,

Could anyone please tell me what the difference would be of inheriting  
a class versus passing that class\object through the initialize  
method?

class ErrorBox \< Box

end

v

box ||= Box.new

class ErrorBox  
def initialize(box)

end

end

And why would I do one over the other?

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 7, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/inheritance-v-passing-object-through-the-constructor/142379/2 "2008-07-07T14:35:39Z")

</div>

On Jul 7, 7:37 am, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> v  
> And why would I do one over the other?
> 
> Thanks
> 
> Aidy

Try googling “inheritance vs delegation” and “inheritance vs  
composition”  
Here is a discusin from Perl ([http://www.perlmonks.org/index.pl](http://www.perlmonks.org/index.pl)?  
node\_id=278375), looks to cover the  
area pretty good

Artima has a lot of good info, here is an article on inheritence vs  
composition (similar to delegation),  
[http://www.artima.com/designtechniques/compoinh.html](http://www.artima.com/designtechniques/compoinh.html)

cheers
