# Calling child from parent

**URL:** <https://www.ruby-forum.com/t/calling-child-from-parent/79548>\
**Category:** Ruby\
**Created:** [December 16, 2006, 6:33am UTC](https://www.ruby-forum.com/t/calling-child-from-parent/79548 "2006-12-16T06:33:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 16, 2006, 6:33am UTC](https://www.ruby-forum.com/t/calling-child-from-parent/79548/1 "2006-12-16T06:33:16Z")

</div>

hi -

i understand super passes method calls up to the parent.

is there a way to call down to the child instance, to a different  
method?

I realize i could pass a function block up to super, and then use  
yield to call back to the child, but i want to hide this complexity  
from my child classes.

the setup is something like this:

class Webserver \< HTTPServlet::AbstractServlet  
def do\_GET(request, response)  
// here i want to hand off to the instance child servlet to  
handle the response  
child.render()  
end  
end

class Servlet1 \< Webserver  
def render  
// do some stuff  
end  
end

etc for other servlets.

I guess the other way is to instantiate the right child servlet in the  
webserver, but this means i dont get access to super methods…

tx,

## /dc

```
  David "DC" Collier

```

[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+81 (0)80 6521 9559  
skype: callto://d3ntaku

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 16, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/calling-child-from-parent/79548/2 "2006-12-16T15:31:39Z")

</div>

“dc” typed:

> i understand super passes method calls up to the parent.  
> is there a way to call down to the child instance, to a different method?  
> I guess the other way is to instantiate the right child servlet in the  
> webserver, but this means i dont get access to super methods…

My OO know-how may have turned rusty, but isn’t a Parent class (and  
therefore its object) oblivous of its children (and therefore the  
methods  
and whatever defined in its children)?

Why not re-define do\_GET in Servlet1?

–  
Ayaz Ahmed K.

“Not only is this incomprehensible, but the ink is ugly and the paper  
is from the wrong kind of tree.”  
– Professor W.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 16, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/calling-child-from-parent/79548/3 "2006-12-16T15:47:40Z")

</div>

On 12/16/06, dc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the setup is something like this:
> 
> class Webserver \< HTTPServlet::AbstractServlet  
> def do\_GET(request, response)  
> // here i want to hand off to the instance child servlet to  
> handle the response  
> child.render()  
> end  
> end

Just (re)define render in the child. Example:

$ irb  
irb(main):001:0\> class Webserver  
irb(main):002:1\> def get  
irb(main):003:2\> puts “In superclass”  
irb(main):004:2\> render  
irb(main):005:2\> end  
irb(main):006:1\> def render  
irb(main):007:2\> puts “In superclass#render”  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> class Servlet \< Webserver  
irb(main):011:1\> def render  
irb(main):012:2\> puts “In subclass#render”  
irb(main):013:2\> end  
irb(main):014:1\> end  
=\> nil  
irb(main):015:0\> Servlet.new.get  
In superclass  
In subclass#render

But remember, a superclass doesn’t know about its subclasses.
