If I have a parent-child that defines some method .foo() like this:
class Parent {
public void foo(Parent arg) {
System.out.println("foo in Function");
}
}
class Child extends Parent {
public void foo(Child arg) {
System.out.println("foo in ChildFunction");
}
}
When I called them like this:
Child f = new Child();
Parent g = f;
f.foo(new Parent());
f.foo(new Child());
g.foo(new Parent());
g.foo(new Child());
the output is:
foo in Parent
foo in Child
foo in Parent
foo in Parent
But, I want this output:
foo in Parent
foo in Child
foo in Parent
foo in Child
I have a Child class that extends Parent class. In the Child class, I want to "partially override" the Parent's foo()
, that is, if the argument arg
's type is Child then Child's foo()
is called instead of Parent's foo()
.
That works Ok when I called f.foo(...)
as a Child; but if I refer to it from its Parent alias like in g.foo(...)
then the Parent's foo(..)
get called irrespective of the type of arg
.
As I understand it, what I'm expecting doesn't happen because method overloading in Java is early binding (i.e. resolved statically at compile time) while method overriding is late binding (i.e. resolved dynamically at compile time) and since I defined a function with a technically different argument type, I'm technically overloading the Parent's class definition with a distinct definition, not overriding it. But what I want to do is conceptually "partially overriding" when .foo()
's argument is a subclass of the parent's foo()
's argument.
I know I can define a bucket override foo(Parent arg)
in Child that checks whether arg's actual type is Parent or Child and pass it properly, but if I have twenty Child, that would be lots of duplication of type-unsafe code.
In my actual code, Parent is an abstract class named "Function" that simply throws NotImplementedException()
. The children includes "Polynomial", "Logarithmic", etc and .foo() includes things like Child.add(Child), Child.intersectionsWith(Child), etc. Not all combination of Child.foo(OtherChild) are solvable and in fact not even all Child.foo(Child) is solvable. So I'm best left with defining everything undefined (i.e. throwing NotImplementedException) then defines only those that can be defined.
So the question is: Is there any way to override only part the parent's foo()? Or is there a better way to do what I want to do?
EDIT:
@Zeiss: If I use Double Dispatch, like this:
class Parent {
public void foo(Parent arg) {
System.out.println("foo in Parent");
}
}
class Child extends Parent {
public void foo(Parent arg) {
System.out.println("foo in Child(Parent)");
arg.foo(this);
}
public void foo(Child arg) {
System.out.println("foo in Child(Child)");
}
}
I got infinite recursion:
(stack):
StackOverflowError: ...
...
at sketch_apr25a$Child.foo(sketch_apr25a.java:35)
...
(output):
...
foo in Child(Parent)
...
when executing g.foo(new Child());
. The rest seems to be fine, as the output is:
foo in Child(Parent)
foo in Parent
foo in Child(Child)
foo in Child(Parent)
foo in Parent
foo in Child(Parent)
(infinite recursion follows)
Why do this happen? g is the Parent's alias, but it's accessing Child's foo(Parent)?