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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:27:23+00:00 2026-05-13T16:27:23+00:00

i have a math class that stores representation of expressions in n-array lists i

  • 0

i have a math class that stores representation of expressions in n-array lists
i currently have three classes AdditionArray MultipleArray and Variable they all implement my Number interface.

public interface Number {

  public Number Multiply(Number number);

in the classes that implement Number i have overloaded opertions like Multiply for example

public class MultipleArray extends ArrayList<Number> implements Number{
  public Number Multiply(AdditionArray number);
  public Number Multiply(Number number){throw new Exception("woops");}

the thing is java is not automatically calling the correct overloaded function at runtime. it seams to be figuring it out at compile time

for example

Number someNumber = new MultipleArray();
Number someOtherNumber = new AdditonArray();

MultipleArray result2 = someNumber.Multiply(someOtherNumber); //calls the correct function

Number result2 = someNumber.Multiply(someOtherNumber); // throws the woops exception

why is java doing this. and is there another way i can implement this. some sort of factory for example?

Cheers,
Mark

  • 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-13T16:27:23+00:00Added an answer on May 13, 2026 at 4:27 pm

    As Bill K said, Java determines this at compile time, and this is by design.

    The really standard way to implement this is to have code in the overloaded method with the supertype that checks the type of the subclass and delegates with a cast or throws an exception as appropriate.

    Using Generics is another option, which will improve your type safety, but I don’t know enough about your code to say if it would work all around. A generic number interface might look like this:

     public interface Number<T extends Number> {
           public T multiply(Number<? extends T> number);
     }
    

    Generics can get weird around the edges, so it might be worth it to just give up on the type safety and go with checking the type of the parameter.

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

Sidebar

Related Questions

I have a simple POCO class that contains the student's scores. For example: Math
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have an ASP.NET page that is interacting with a business class. I want
For a math package, I am trying to have classes for different types of
I'm writing a series of math based class that each inherit from an abstract
I have a python class that generates the nth prime number by starting at
I have a small math problem I am trying to solve Given a number
i have some xml with math ml inside, and i would like to find
I have a C library with numerous math routines for dealing with vectors, matrices,
If I have a string with a valid math expression such as: String s

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.