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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:57+00:00 2026-05-26T01:54:57+00:00

I learned that I can use the real type of a Object to define

  • 0

I learned that I can use the real type of a Object to define which Method is used, such like this:

[...]
Object foo = new String("hello");
[...]
bla(foo);

void bla(String x){
}
void bla(Integer x){
}

Now it should take the bla(String x) method, but instead I get compiler error. I know why: because the type of foo is currently Object. I’m not sure if I do it currently right, but I understood it that way, that Java will choose the method by the real type (or specific type), so if I downcast it to Object, it will choose the String instead, if no Object Method is specified.

Or is the only way to determinate the type by if(foo instanceof xxx) in a method void bla(Object x)?

P.S.: dont get me wrong on this: I dont mean that I can overload methods, I mean that I want to choose the method based on the real type (not on the defined one)!

  • 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-26T01:54:58+00:00Added an answer on May 26, 2026 at 1:54 am

    Now it should take the bla(String x) method, but instead i get compiler error.

    Right. Which method is to be called at runtime, is chosen at compile-time, and the compiler can’t (in general) tell which type foo will have in runtime.

    …so if i downcast it to Object…

    You can’t “downcast to Object”. You can downcast to a String though. And that’s right, if you do

    bla((String) foo);
    

    it will invoke the bla that takes a String as argument (and throw a ClassCastException if foo is not a String).

    Or is the only way to determinate the type by if(foo instanceof xxx) in a Method void bla(Object x)?

    […]

    i mean that i want to choose the method based on the real type (not on the defined one)!

    This is usually referred to as double or multiple dispatch, and is a feature not supported in Java.

    Implement the visitor pattern if you really need this.


    Btw, what you refer to as “defined type” and “real type” are usually referred to as static type and runtime type respectively. That is, Object is the static type of foo, while String is it’s runtime type.

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

Sidebar

Related Questions

I've learned that MySQL can compress communication between servers and clients. Compression is used
I learned about location tag that can be used to disable the inheritance for
I learned that copy something to kill buffer, I can use the kill-new buffer
I have recently learned that you can use a neat switch statement with fallthrough
I learned that Xcode can compile c code, that means can we write whole
From what I learned, a MySQL MASTER server is one that can both read
I just learned (the hard way) that Java Component s can only have one
I have learned from various tutorial that If a client can reasonably be expected
I learned that when executing commands in Python, I should use subprocess. What I'm
I learned that repaint() wasn't an actual method that repaints the screen. Rather, it

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.