Solved

Class Instance Related Question

Posted on 2004-08-30
14
214 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Enhanced For loop vs ForEach Loop 4 56
Fibonacci challenge 11 82
unix in java example 9 39
Java JRE greater than 1.6 5 13
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now