Solved

Class Instance Related Question

Posted on 2004-08-30
14
221 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

707 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