• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Class Instance Related Question

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
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 6
  • 3
  • 3
  • +1
3 Solutions
 
CEHJCommented:
Strange - shouldn't compile (duplicate constructors)
0
 
CEHJCommented:
I think you may have posted the wrong code ;-)
0
 
sudhakar_koundinyaAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudhakar_koundinyaAuthor Commented:
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
 
CEHJCommented:
It will call the ctor using the closest match afaik. String matches String more closely than CharSequence
0
 
RuadRauFlessaCommented:
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
 
RuadRauFlessaCommented:
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
 
CEHJCommented:
>>unless what you have passed it is a child instance of what the method asks for

We're talking about interfaces here (CharSequence)
0
 
RuadRauFlessaCommented:
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
 
objectsCommented:
> 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
 
CEHJCommented:
(Which is what i already said)
0
 
sudhakar_koundinyaAuthor Commented:
Oh, I C

I understand that
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
Let us know if you have any more questions.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now