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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:12:17+00:00 2026-06-09T17:12:17+00:00

In Java you can put in multiple constructors to a class that are called

  • 0

In Java you can put in multiple constructors to a class that are called depending on the types and/or number of parameters that are used when an instance is constructed.
I assume that there is the equivalent in Objective C.

Can I have a polymorphic method?

I would like to build a method that acts slightly differently according to whether a string is passed or a double?

Does that sound bonkers or is it easy?

  • 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-09T17:12:18+00:00Added an answer on June 9, 2026 at 5:12 pm

    You’re thinking of overloaded methods. Due to the way dynamic dispatch is implemented in Objective-C, it isn’t currently possible to pass two unrelated types as arguments to the same (or same-named) method and have it understand.

    In Objective-C, there are two related but distinct approaches to handling multiple kinds of input. Let’s use your example of a string or a double as possible inputs. In Java, you might have:

     void applyWidget(String s);
     void applyWidget(double d);
    

    And that’s great, but not Objective-C. In Objective-C, you instead would use two different method names:

    - (void)applyWidgetWithName: (NSString *)name;
    - (void)applyWidgetWithValue: (double)value;
    

    The same logic is in each method as in the Java version, but the distinct names let the compiler treat them as distinct methods (which they are, even in Java.) The code also becomes self-documenting: by reading it, you get an idea of what’s happening even without comments. Alternatively, if you simply must have one method name, you change the parameter type to id and accept any object:

    - (void)applyWidget: (id)widget;
    

    Then pass either an NSString or an NSNumber wrapping your double. Then, in the implementation of the method, use Objective-C’s introspection methods to determine how to proceed:

    if ([widget isKindOfClass: [NSString class]]) {
        ...
    } else if ([widget isKindOfClass: [NSNumber class]]) {
        double d = [widget doubleValue];
        ...
    }
    

    This approach essentially tells callers “send anything–I’ll handle it appropriately.” It can be difficult to determine the behaviour of such a method without extensive documentation.

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

Sidebar

Related Questions

I have a common library where I've put classes that are used between multiple
I've built a Selector-based system in Java that can accept multiple clients. It has
With the regular statement (just statement) I can put java enums into the queries
How can i put this string in to a cell using java apache poi?
In java can an instance variable and a method have the same name without
Is there anything simple Java can't do that can be done in a similar
I'm looking for solutions, preferably open source, that can manage deployments of multiple interdependent
If I have a java project that consists of several different types of files
I'm trying to create a JAR that I can put on a non development
My goal is to put multiple java.io.File objects into a zip file and print

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.