Solved

Class Instance Related Question

Posted on 2004-08-30
14
219 Views
Last Modified: 2010-03-31
Hello All,

This may be silly doubt. I just wanted to have clarification on this. So don't laugh at me hehhehehe

class Test
{
    Test(String str,CharSequence str1)
     {
           System.err.println("Hello I am in Constructor that calls CharSequence as it's one of It's parameters");
     }

    Test(String str,CharSequence str1)
     {
           System.err.println("Hello I am in Constructor that calls String as it's second argument");
     }
    public static void main(String s[])
    {
            new Test("Hello","World");
    }
}

Output: Hello I am in Constructor that calls String as it's second argument


I am under impression that JVM calls the first constructor. But JVM called second constructor.

So can u clarify, when such situation arises, why it uses current class's instance but not super class or implemented interface ??

thanks
Sudhakar

0
Comment
Question by:sudhakar_koundinya
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11933709
Strange - shouldn't compile (duplicate constructors)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11933715
I think you may have posted the wrong code ;-)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11933719
Sedonc Test case:

class Test
{
    Test(String str,CharSequence str1)
     {
           System.err.println("Hello I am in Constructor that calls CharSequence as it's one of It's parameters");
     }

    public static void main(String s[])
    {
            new Test("Hello","World");
    }
}

Output:Hello I am in Constructor that calls CharSequence as it's one of It's parameters
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11933760
I have posted the wrong code

sorry

that should be

class Test
{
    Test(String str,CharSequence str1)
     {
           System.err.println("Hello I am in Constructor that calls CharSequence as it's one of It's parameters");
     }

    Test(String str,String str1)
     {
           System.err.println("Hello I am in Constructor that calls String as it's second argument");
     }
    public static void main(String s[])
    {
            new Test("Hello","World");
    }
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 11933820
It will call the ctor using the closest match afaik. String matches String more closely than CharSequence
0
 
LVL 3

Assisted Solution

by:RuadRauFlessa
RuadRauFlessa earned 50 total points
ID: 11933917
Well it is going to call which ever one for which you provide the correct types of paramaters.

Say you pass it a String and then a CharSequence then it will call the first one. If you pass it two Strings then it will take the second. The later one is the case in your main method so that would be why it calls the second in stead of the first. It is called Overloading and can be done with any method as long as you do not provide two methods within the same class which has the same data types in the same order. Say for instance you have two constructors that both took two Strings then you would get a compi.e time error. Now if you were to add another paramater to one of the constructors it won't be a problem will it.

In any case you also asked something about a super class' constructor. Well there are basically two cases where it will in fact not call the parent constructor and that is when you have Overloaded the constructor and called the overloaded one and when you override the super's constructor with your own implimentation thereof. It is usually good practice to actually call the super() (parent constructor) before proceding with custom initialization as to make sure that you keep the functionality held by its parent class.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11933974
CEHJ if you pass it something it has to match exactly unless what you have passed it is a child instance of what the method asks for.  If you pass it a child it will automatically cast it otherwise you will have a no go.


http://java.sun.com/j2se/1.4.2/docs/api/java/lang/NoSuchMethodError.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11934047
>>unless what you have passed it is a child instance of what the method asks for

We're talking about interfaces here (CharSequence)
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11934399
Yeah I know so if it does find an exact match it will then use that one instead of the other one which needs casting.

Thw whole Idea behind overloading it to be able to abstract the functionality to such an extent that one can supply the data needed for it in various formats and still reach the desired result.
0
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 11937015
> We're talking about interfaces here (CharSequence)

thats irrelevant.

It simply looks for the best match, whether its a class, or interface involved makes no difference.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11937036
(Which is what i already said)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11940901
Oh, I C

I understand that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11944806
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11946785
Let us know if you have any more questions.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question