# Newbie question about initialize methods in subclasses

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-initialize-methods-in-subclasses/94673>\
**Category:** Ruby\
**Created:** [May 10, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/newbie-question-about-initialize-methods-in-subclasses/94673 "2006-05-10T19:09:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 10, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/newbie-question-about-initialize-methods-in-subclasses/94673/1 "2006-05-10T19:09:41Z")

</div>

All,

I just seem to have had a revelation about how the super keyword in Ruby  
is ever so slightly different from the super keyword in Java.

In general, given

class B \< A

and desiring to add an initialize method to B, is it a “best practice”  
to always do

class B  
…  
def initialize  
super  
… custom B initialization here …  
end

Basically, I’m trying to understand that if I instantiate B, then  
A.initialize doesn’t get called by default - is that correct? Is that  
so that B can completely override the initialization details of itself?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 10, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/newbie-question-about-initialize-methods-in-subclasses/94673/2 "2006-05-10T19:24:14Z")

</div>

Wes G. wrote:

> All,
> 
> I just seem to have had a revelation about how the super keyword in Ruby  
> is ever so slightly different from the super keyword in Java.
> 
> In general, given
> 
> class B \< A
> 
> and desiring to add an initialize method to B, is it a “best practice”  
> to always do
> 
> class B  
> …  
> def initialize  
> super  
> … custom B initialization here …  
> end
> 
> Basically, I’m trying to understand that if I instantiate B, then  
> A.initialize doesn’t get called by default - is that correct? Is that  
> so that B can completely override the initialization details of itself?
> 
> Thanks,  
> Wes

A.intialize does not get called by default - you do have to call super.  
This is not unique to the initialize method AFAIK. If you override any  
method with the same signature, you must call super to invoke the  
previous version (up the chain).

BTW, Ruby for Rails by David Black has a very clear description of  
method lookup that I just happened to be reading this morning. So its  
his fault if I’m wrong about this 🙂  
Keith

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 10, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/newbie-question-about-initialize-methods-in-subclasses/94673/3 "2006-05-10T19:38:04Z")

</div>

Upon further reflection, my entire problem boiled down to not  
understanding the “super” keyword in Ruby, which means “execute the same  
method in my superclass”, not “the superclass object” (which is what it  
means in Java).

Which, practically speaking, is more what you want anyway (I can’t think  
of a reason that you would ever want to sling around an instance of a  
superclass, but I’ll have to consider that).

Thanks,  
Wes

Keith L. wrote:

> Wes G. wrote:
> 
> > All,
> > 
> > I just seem to have had a revelation about how the super keyword in Ruby  
> > is ever so slightly different from the super keyword in Java.
> > 
> > In general, given
> > 
> > class B \< A
> > 
> > and desiring to add an initialize method to B, is it a “best practice”  
> > to always do
> > 
> > class B  
> > …  
> > def initialize  
> > super  
> > … custom B initialization here …  
> > end
> > 
> > Basically, I’m trying to understand that if I instantiate B, then  
> > A.initialize doesn’t get called by default - is that correct? Is that  
> > so that B can completely override the initialization details of itself?
> > 
> > Thanks,  
> > Wes
> 
> A.intialize does not get called by default - you do have to call super.  
> This is not unique to the initialize method AFAIK. If you override any  
> method with the same signature, you must call super to invoke the  
> previous version (up the chain).
> 
> BTW, Ruby for Rails by David Black has a very clear description of  
> method lookup that I just happened to be reading this morning. So its  
> his fault if I’m wrong about this 🙂  
> Keith
