method overloading

Discussion in 'Java' started by David, Apr 10, 2004.

  1. David

    David Guest

    Hi guys,
    I have two classes "Mother" and "Daughter".
    Mother is an abstract class with a public method "foo()" implemented

    "Daughter" extends Mother and implements a public method "foo()"
    I want the foo() method from Daughter to perform some calculations then
    call the implementation from "foo()" in the class Mother.

    Is this possible, if yes, how please? I've tried it, but i've ended in an
    infinite recursive call. Here's the dumb code:

    //------------------------
    abstract class Mother {
    protected int x;
    public void foo() {
    System.out.println("x: " + x);
    }
    }

    class Daughter extends Mother {
    public void foo() {
    x = 3;
    ((Daughter) this).foo();
    }
    }

    public class Test {
    public static void main(String[] args) {
    Daughter d = new Daughter();
    d.foo();
    }
    }
    //------------------------

    Fanxs for your help.

    _
    David Hitillambeau
    David, Apr 10, 2004
    #1
    1. Advertising

  2. David wrote:

    > Hi guys,
    > I have two classes "Mother" and "Daughter".
    > Mother is an abstract class with a public method "foo()" implemented
    >
    > "Daughter" extends Mother and implements a public method "foo()"
    > I want the foo() method from Daughter to perform some calculations then
    > call the implementation from "foo()" in the class Mother.
    >
    > Is this possible, if yes, how please? I've tried it, but i've ended in an
    > infinite recursive call. Here's the dumb code:
    >
    > //------------------------
    > abstract class Mother {
    > protected int x;
    > public void foo() {
    > System.out.println("x: " + x);
    > }
    > }
    >
    > class Daughter extends Mother {
    > public void foo() {
    > x = 3;
    > ((Daughter) this).foo();
    > }
    > }
    >
    > public class Test {
    > public static void main(String[] args) {
    > Daughter d = new Daughter();
    > d.foo();
    > }
    > }


    You need to learn about the "super" keyword.
    It can be used in 2 ways:

    - to call a method in the superclass, which is what you need:
    So in Daughter, you need to call Mother's foo like this:

    public void foo() {
    //do stuff
    //call super classes foo method
    super.foo();
    }

    - to call a constructor of the superclass:

    public Daughter() {
    super("I pass this to the Mother constructor");
    }

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Apr 10, 2004
    #2
    1. Advertising

  3. David wrote:

    > Hi guys,
    > I have two classes "Mother" and "Daughter".
    > Mother is an abstract class with a public method "foo()" implemented
    >
    > "Daughter" extends Mother and implements a public method "foo()"
    > I want the foo() method from Daughter to perform some calculations then
    > call the implementation from "foo()" in the class Mother.
    >
    > Is this possible, if yes, how please? I've tried it, but i've ended in an
    > infinite recursive call. Here's the dumb code:


    <snip code>

    One more thing:

    It's better to ask the newbie kind of questions in c.l.j.help instead.

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Apr 10, 2004
    #3
  4. David

    Roedy Green Guest

    On Sat, 10 Apr 2004 20:34:41 +0400, David
    <> wrote or quoted :

    >I have two classes "Mother" and "Daughter".
    >Mother is an abstract class with a public method "foo()" implemented
    >
    >"Daughter" extends Mother and implements a public method "foo()"
    >I want the foo() method from Daughter to perform some calculations then
    >call the implementation from "foo()" in the class Mother.


    see http://mindprod.com/jgloss/gotchas.html and search for the word
    "recipe". It will explain the various types of overloading and
    shadowing with a large example.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 10, 2004
    #4
  5. David

    David Guest

    Thank you for your kind responses and i apologize for being
    off-topic.

    _
    David Hitillambeau
    David, Apr 11, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,396
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    423
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    571
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    391
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page