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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:08:26+00:00 2026-06-02T02:08:26+00:00

Since java is an object orientated language it should exhibit polymorphism. Below is my

  • 0

Since java is an object orientated language it should exhibit polymorphism. Below is my definitions of a type of polymorphism; ad-hoc polymorphism, and a subtype of that; coercion.

Ad-hoc polymorphism is obtained when a function works, or appears to work, on several different types (which may not exhibit a common structure) and may behave in unrelated ways for each type. There are two types of ad-hoc polymorphism, coercion and overloading.

Coercion is a semantic operation that avoids a type error. The compiler converts one type into another in order to match an argument type in a function call to the parameter type in the function definition. The function definition only works on one type. Compilers implement coercion at compile time.

I have this example working in C++

#include <iostream>
 using namespace std;

 void display(int a) const
 {
     cout << "One argument (" << a
          << ')' << endl;
 }

 int main( )
 {
     display(10); // returns "One argument (10)"
     display(12.6); // narrowing // returns "One argument (12)"
 }

Im trying to implement the same program in java without success.

public static void display (int i)
{
  System.out.println("One argument (" + i + ")");
}

public static void main (String[] args)
{
  display(10); // One argument (10)
  display(12.6); // Narrowing (a type of coercion) takes place. One argument (12)
}

but I am receiving the error.

The method display is not applicable for the arguments(double).

Do you know how to convert successfully. Please be aware that I really wish to use coercion technique where the compiler fixes the types automatically. So i casting to int with (int) 12.6 is not an option for me.

If you have another coercion example which exhibits narrowing, I would be grateful if you shared it with me 🙂

Regards.

  • 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-02T02:08:28+00:00Added an answer on June 2, 2026 at 2:08 am

    Java doesn’t allow narrowing coercion but just widening coercion.

    So there are allowed:

    float foo(float f) { return f*f; }
    
    int i = 10;
    float f = foo(i);
    float f2 = i;
    

    But not when you are going to lose precision.

    EDIT: Actually since a float in java is a standard 4 byte IEEE754 you do actually lose precision but it is allowed in any case, this shows this behavior:

    int d = Integer.MAX_VALUE;
    float f = d;
    System.out.println(String.format("%d != %12.0f", d, f));
    

    You’ll have:

    2147483647 != 2147483648
    

    So the answer is a little bit more fuzzy than it appears.

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

Sidebar

Related Questions

Java's Properties object hasn't changed much since pre-Java 5, and it hasn't got Generics
I've been told recently a good practice in object oriented programming that you should
What is an alternative function for the goto keyword in Java? Since Java does
I haven't touched Java since using JBuilder in the late 90's while at University,
What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting)
I must use pkzip in my java program to zip (since standard java zip
It has been a long time since I used java and I have run
The Java Memory Model (since 1.5) treats final fields differently to non- final fields.
Since JavaScript is not derived from Java, why does it have Java in the
Since uni, I've programming in Java for 3 years, although I am not fully

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.