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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:36:53+00:00 2026-05-14T06:36:53+00:00

Given I have a class with two constructors: public class TestClass { ObjectOne o1;

  • 0

Given I have a class with two constructors:

public class TestClass {
    ObjectOne o1;
    ObjectTwo o2;

    public TestClass(ObjectOne o1) {
        // ..
    }

    public TestClass(ObjectTwo o2) {
        // ..
    }
}

Please assume, that ObjectOne is an interface type, and ObjectTwo implements ObjectOne. What happens, if I call:

new TestClass(null);

How to determine the correct method to call? And who determines that?
Are there differences between Java and other OOP languages?

  • 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-14T06:36:53+00:00Added an answer on May 14, 2026 at 6:36 am

    This question is really about resolving ambiguous overloads, and not really about run-time polymorphism (because choosing which overloaded method to invoke is done at compile-time).

    Method resolution is a complicated thing, and in this case, whether or not the code compiles at all does depend on what the types involved are. There are plenty of situations in which the code will compile. See code below (note that String implements CharSequence):

    public class MyClass {
        MyClass(CharSequence charSeq) {
            System.out.println("CharSequence");
        }
        MyClass(String s) {
            System.out.println("String");
        }   
        public static void main(String[] args) {
            new MyClass(null); // prints "String"
            new MyClass(""); // prints "String"
            new MyClass((CharSequence) null); // prints "CharSequence"
            new MyClass((CharSequence) "");   // prints "CharSequence"
        }
    }
    

    Note that without the cast, the String overload is chosen. This is exactly as specified in JLS:

    JLS 15.12.2.5 Choosing the Most Specific Method

    If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

    A String is-a CharSequence, but not all CharSequence is-a String. Therefore, String is more specific than CharSequence, hence why the String overload is chosen in the above example.


    It is worth noting that this exact question (in essence) appeared in the wonderful Java Puzzlers (highly recommended), specifically Puzzle 46: The Case of the Confusing Constructor

    Java’s overload resolution process operates in two phases. The first phase selects all the methods or constructors that are accessible and applicable. The second phase selects the most specific of the methods or constructors selected in the first phase. One method or constructor is less specific than another if it can accept any parameters passed to the other

    The key to understanding this puzzle is that the test for which method or constructor is most specific does not use the actual parameters: the parameters appearing in the invocation. They are used only to determine which overloadings are applicable. Once the compiler determines which overloadings are applicable and accessible, it selects the most specific overloading, using only the formal parameters: the parameters appearing in the declaration.


    I will close with a quote from Effective Java 2nd Edition, Item 41: Use overloading judiciously:

    The rules that determine which overloading is selected are extremely complex. They take up thirty-three pages in the language specification, and few programmers understand all of their subtleties.

    Needless to say, this book is also highly recommended.

    See also

    • Polymorphism vs Overriding vs Overloading
    • Method Overloading. Can you overuse it?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 519k
  • Answers 519k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, it is faster, because you only compare exactly one… May 16, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer You could write a simple PHP script to execute on… May 16, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer There are 2 possible causes of the flicker. The first… May 16, 2026 at 8:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Morning. Issue: I have a class called Reports. Two constructors. One allows no parameters,
I have SalesMan class which is inherited from User class and also implements ISalesMan
Given the following template: template <typename T> class wrapper : public T {}; What
In a visual studio 2008 solution, I have two versions of the same class
I have the following relationship between two domain classes: class Emp { String name
I need the following functionality Given two sorted lists, merge them I have this
I have several classes that need to load some properties files, and I was
You know, like Battlestar paper! I have given this a few goes but now
I have a JSON multi-level response that I need to deserialize and from the
Assuming you had some kind of factory-created resource that would still belong to the

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.