Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:52:00+00:00 2026-05-14T08:52:00+00:00

If I have a parent-child that defines some method .foo() like this: class Parent

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:52:01+00:00Added an answer on May 14, 2026 at 8:52 am

    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()); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's get_headers($url), not get_headers($fp). Unless i'm reading it totally wrong… May 14, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer By running your program through strace: strace path/to/your/executable e.g. strace… May 14, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer $a = array(1, 2, 3, 4, 3, 3, 4, 4,… May 14, 2026 at 8:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.