views:

230

answers:

2

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)?

+2  A: 

Isn't this a use case for Double Dispatching ?


Update:

class Function {
    public void add(Function f) {
        f.addMe(this);
    }

    public void addMe(Function f) {
        // Default case
        throw NotImplementedException();
    }
    public void addMe(Logarithmic log) {
        // Code that handles the real
    }
}

class Logarithmic extends Function {
    // This object supports adding
    public void add(Function f) {
        f.addMe(this);
    }
}


Logarithmic log = new Logarithmic();
log.add(new Function()); 
log.add(new Logarithmic()); 

Function f = log;
f.add(new Function()); 
f.add(new Logarithmic()); 
ZeissS
+1 you are right, this is a better approach.
Péter Török
I've thought about Double Dispatch, but that gives me an infinite recursion. I'm probably not using it correctly though, please check the updated question.
Lie Ryan
In your edit you overwrite (in Child) the method in the parent, thus you call yourself. Your problem is a bit more complex than the Double Dispatch Pattern describes in the Wikipedia, since you already pass an object as a parameter. See my example code I tried to work out. Maybe this helps you a bit.
ZeissS
I've tried a few variations on that, but all that works requires the Function to know about the Logarithmic, Polynomials, etc. The use of Double Dispatch is so the Parent (or more generally the service provider) doesn't have to know about who their Childs are (more generally the service user), so I think it's close to what I want; I'll keep "experimenting" (for lack of conscious designing, too tired to think right now) and keeps this thread updated.
Lie Ryan
A: 

I got it to work by explicitly overriding foo(Parent arg) in Child like this -

class Parent {
    public void foo(Parent arg) {
        System.out.println("foo in Parent");
    }
}

class Child extends Parent {
    @Override
    public void foo(Parent arg) {
        System.out.println("foo in Child(Parent)");
        if (arg instanceof Child) {
            foo((Child)arg);
        } else {
            super.foo(arg);
        }
    }
    public void foo(Child arg) {
        System.out.println("foo in Child(Child)");
    }
}

This seems to match the logic of

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().

But rather than "partially override" the method, you actually have to override the method.

Nate
But if I have (say) twenty child, that mean I'd have to copy this override code to twenty child, this is what I want to avoid when I say "lots of duplication of type-unsafe code". If the type-unsafe code is localized to a single point (say, Parent), it might be acceptable. (and yes, I consider `instanceof` type-unsafe, since it meddles with the type system)
Lie Ryan