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 7684349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:15+00:00 2026-05-31T19:00:15+00:00

I have a collection (or list or array list) in which I want to

  • 0

I have a collection (or list or array list) in which I want to put both String values and double values. I decided to make it a collection of objects and using overloading ond polymorphism, but I did something wrong.

I run a little test:

public class OOP {
    void prova(Object o){
        System.out.println("object");
    }

    void prova(Integer i){
    System.out.println("integer");
    }

    void prova(String s){
        System.out.println("string");
    }

    void test(){
        Object o = new String("  ");
        this.prova(o); // Prints 'object'!!! Why?!?!?
    }

    public static void main(String[] args) {
        OOP oop = new OOP();
        oop.test(); // Prints 'object'!!! Why?!?!?
    }
}

In the test seems like the argument type is decided at compile time and not at runtime. Why is that?

This question is related to:

Polymorphism vs Overriding vs Overloading
Try to describe polymorphism as easy as you can

EDIT:

Ok the method to be called is decided at compile time. Is there a workaround to avoid using the instanceof operator?

  • 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-31T19:00:16+00:00Added an answer on May 31, 2026 at 7:00 pm

    This post seconds voo’s answer, and gives details about/alternatives to late binding.

    General JVMs only use single dispatch: the runtime type is only considered for the receiver object; for the method’s parameters, the static type is considered. An efficient implementation with optimizations is quite easy using method tables (which are similar to C++’s virtual tables). You can find details e.g. in the HotSpot Wiki.

    If you want multiple dispatch for your parameters, take a look at

    • groovy. But to my latest knowledge, that has an outdated, slow multiple dispatch implementation (see e.g. this performance comparison), e.g. without caching.
    • clojure, but that is quite different to Java.
    • MultiJava, which offers multiple dispatch for Java. Additionally, you can use
      • this.resend(...) instead of super(...) to invoke the most-specific overridden method of the enclosing method;
      • value dispatching (code example below).

    If you want to stick with Java, you can

    • redesign your application by moving overloaded methods over a finer grained class hierarchy. An example is given in Josh Bloch’s Effective Java, Item 41 (Use overloading judiciously);
    • use some design patterns, such as Strategy, Visitor, Observer. These can often solve the same problems as multiple dispatch (i.e. in those situations you have trivial solutions for those patterns using multiple dispatch).

    Value dispatching:

    class C {
      static final int INITIALIZED = 0;
      static final int RUNNING = 1;
      static final int STOPPED = 2;
      void m(int i) {
        // the default method
      }
      void m(int@@INITIALIZED i) {
        // handle the case when we're in the initialized `state'
      }
      void m(int@@RUNNING i) {
        // handle the case when we're in the running `state'
      }
      void m(int@@STOPPED i) {
        // handle the case when we're in the stopped `state'
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a collection (be it an array, generic List, or whatever is
I have a list/collection of objects that may or may not have the same
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have a my custom collection derived from List(.MyItem.) I want to trace if
I have a list of objects and I want to sent it thought TCP
A typed array implements both the System.Collections.IList and System.Collections.Generic.ICollection<T> interfaces, which both have their
I have a form which fills some objects of a Collection. I had the
I have a collection of custom entity objects one property of which is an
i have a array list for using it in spinner ,i have first value
I have Collection List<Car> . How to compare each item from this collection with

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.