[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Class Instance Related Question

Posted on 2004-08-30
14
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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 200 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 200 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 100 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

650 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