calling child from parent

D

dc

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
 
A

Ayaz Ahmed Khan

"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 Khan

"Not only is this incomprehensible, but the ink is ugly and the paper
is from the wrong kind of tree."
-- Professor W.
 
S

Simen Edvardsen

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top