From: Robert Klemme Date: 2011-11-07T18:09:09+09:00 Subject: Re: Newbie:: Understand Constructor Flow. On Mon, Nov 7, 2011 at 4:32 AM, Gennady Bystritsky wrote: > On Nov 6, 2011, at 7:16 PM, Ankush Ganatra wrote: > >> I am a little confused and need some help. I am a newbie for Ruby but i think i understand Object Oriented programming a little bit so i got confused here. >> >> I have got 2 classes here >> >> 1. CEmployee >> >> class CEmployee >>     def initialize() >>         puts "inside CEmployee" >>     end >> end >> >> 2. CManager >> >> class CManager < CEmployee >>     def initialize() >>         puts "inside CManager" >>     end >> end >> >> Now if i create an Object of CManager class (unlike other languages like C++ ) the constructor of it gets called but not the base class "Until i use the super keyword". >> >>     objManager = CManager.new >> >> Can someone please help me here? And even if the this is supposed to be working like this, isn't it something different from the Object Oriented Concept which says base class constructor always gets called first? > > This is how it is supposed to work indeed, in Ruby there's no notion of default constructor like in C++ (a non-default constructor must be invoked explicitly even there). With a call to super, a subclass can choose how a superclass constructor is invoked in regards to passed parameters and the order of statements. Or not to invoke it at all. Ankush, an additional note: super is different than ordinary method calls with regard to brackets. If you just write "super" the complete argument list of the current method is copied (including block if passed); regular methods are invoked with empty argument list in this case. If you want to explicitly invoke the super class method without arguments you need to write "super()". Providing specific arguments works as well of course: "super(1, 'foo')". Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/