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

  • SEARCH
  • Home
  • 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 8989701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:19:34+00:00 2026-06-15T22:19:34+00:00

This problem presented itself when me and my friends were studying for exams. We

  • 0

This problem presented itself when me and my friends were studying for exams. We noticed strange behavior when calling a statically assigned variable’s method.

Code>Words so here we go:

class C {

    public void a(double x) {}
}

class D extends C {
    void b() {} 
}

class F extends D {

    void a(int i) {} 
    void a(double d) {} 

Now, doing

D foo = new F();
foo.a(1);

What will this give? Well.. it runs method a(double) in F !!

This is what we thought happened:

  1. Program start by looking for a in static type D: nothing there.
  2. Goes to its super class, C. Does not find a(int) but instead finds a(double).
  3. Decides that this is the signature i meant (i.e. a(double)), but FIIRST, after all this search let’s look at the dynamic type first it says!
  4. Runs a(double) in F!

Is this correct? This means that it climbed the hierarchy to find some method that could fit if type conversion from int to double is done.. AFTER this it checks to see if the dynamic type has this newly interpreted signature.

I noticed that if I added

void a(int) {}

** in class C, it would give me a(int) in F when running the invocation above!

Can someone please explain the mechanics involved in this process? Why is so that the code runs/compiles this way? What’s the technical reasons behind this? And are there any more things one should be aware of concerning similar cirumstances ()

  • 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-06-15T22:19:35+00:00Added an answer on June 15, 2026 at 10:19 pm

    The reason is because Java is statically typed. Dispatch on argument type is done at compile time, not runtime.

    When the code is compiled, the compiler sees that you are invoking a method named a on an object of static type D. It looks for compatible methods in D, and finds a single method (inherited from C). It generates code to perform a virtual call to C.a(double).

    At runtime, the virtual call dispatches on the actual type of the object (not the arguments!), so it ends up calling F.a(double), since this overrides C.a(double).

    The fact that the runtime type of the object is F and F happens to have a different method that would have been valid if it were known at compile time is irrelevant. If you want this behavior, you need reflection.

    If you added C.a(int), the compiler would see two different methods named a in D, and based on the overloading rules, choose the one that takes an int.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is very strange and I'm hoping someone can help me. For the
(This is a question about a vague problem. I try to present all relevant
This problem is same as asked in here . Given a list of coins,
This problem seems very simple to me, but I've been unable to fix it,
This problem is caused by my acknowledge of English and jQuery or maybe something
This problem has been bugging me for a long time. For example, the code
This problem is about concurrent high speed inserts. I must admit it is very
This problem has bugged me so many times and i have now decided to
This problem is happening for me on IE8 and Chrome which makes me think
This problem is baffling me: BEGIN; INSERT INTO sub_users(user_id, email) SELECT user_id FROM users

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.