method overloading

D

David

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
 
C

Christophe Vanfleteren

David said:
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");
}
 
C

Christophe Vanfleteren

David said:
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.
 
R

Roedy Green

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.
 
D

David

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

_
David Hitillambeau
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top