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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:37:56+00:00 2026-05-11T13:37:56+00:00

I have child classes, each carries a different type of value along with other

  • 0

I have child classes, each carries a different type of value along with other members. There may be a LongObject, IntObject, StringObject, etc.

I will be given a value, which can be a long, int, string, etc., and I have to create a LongObject, IntObject, StringObject, etc., respectively.

Would it be faster to overload a method as shown below (a) or just use a elseif as shown below (b)?

It may not be a noticeable performance difference. It may be that the overloaded methods are implemented in a similar manner to the if/else anyway. I don’t know.

I can also hear some of you saying to just test it. Sure, I ought to. I would also like to know how this type of overloading is handled under the hood, if anyone knows.

Please let me know what you think.

Thanks, jbu

a)

BaseObject getObject(long l) {      return new LongObject(l); }  BaseObject getObject(int i) {      return new IntObject(i); }  BaseObject getObject(String s) {      return new StringObject(s); }  ... 

b)

BaseObject getObject(Object x) {     if(value is a long)          return new LongObject((Long)x);     else if(value is an int)          return new IntObject((Int)x);     else if(value is a String)          return new StringObject((String)x);     ... } 

edit: I guess I didn’t completely add all the details, some of you caught it. For both choices, I still have to get an object/value and from the value determine what type it is. Therefore, I still have to do an if/else of some sort to even use the overloaded methods.

  • 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. 2026-05-11T13:37:57+00:00Added an answer on May 11, 2026 at 1:37 pm

    There’s a massive discrepancy here: overloads are chosen at compile-time whereas your ‘if (value is a long)’ would be an execution-time test.

    If you know the type at compile-time, I strongly suggest you use that information. If you don’t, then the overloading option isn’t really feasible anyway.

    EDIT: The comment suggests I elaborate a bit about overloads being chosen at compile-time.

    The compiler picks which method signature is called based on compile-time information about the arguments. This is unlike overriding where the method implementation to use is determined by the type of the actual target of the method.

    Here’s an example:

    public class Test {     public static void main(String[] args)     {         Object x = 'I'm a string';         foo(x);     }      public static void foo(String x)     {         System.out.println('foo(String)');     }      public static void foo(Object x)     {         System.out.println('foo(Object)');     } } 

    This prints foo(Object) because the compile-time type of x is Object, not String. The fact that the execution-time type of the object that x refers to is String doesn’t mean that foo(String) is called.

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

Sidebar

Related Questions

I have an std::list<TYPE> which may contain child classes of TYPE. I need to
Most inheritance questions I've seen assume that the child classes have different attributes than
I have two basic classes that inherits each other: BaseClass: // .h @interface BaseClass
I have one abstract class and many child classes. In child classes are from
I have a MustInherit Parent class with two Child classes which Inherit from the
In Django, when you have a parent class and multiple child classes that inherit
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have two model classes, say parent and child. Parents are created first, and
Let's say I have two classes, A and B, where B is a child
I have a sprite managing class and two other classes, one is a player

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.