File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on constructor" Watch "Question on constructor" New topic
Author

Question on constructor

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class N1
{
String s="-";
N1()
{
this("d");
s+="a";
}
N1(String s)
{
s+="d";
}
}

class B1 extends N1
{
B1()
{
super();
s+="b";
}
}
public class C extends B1 {
C()
{
s+="c";
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println((new C()).s);
}

}

Output is -abc

how come b is calling super constructor which is a in a there is this("d"), which call its argumnet constructor which A(String e).
which has s+="d".

I think output should be -dabc.

Please explain


Thanks<br />Dinesh
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi Dinesh

In your example, you are overshadowing the default variable s with the local variable s in N1(String s). So the values changed here are limited upto that constuctor block only. If you change your syntax to N1(String s1), then you will get the answer -dabc.

HTH
Phalguni
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi Dinesh

In your example, you are overshadowing the default variable s with the local variable s in N1(String s). So the values changed here are limited upto that constuctor block only. If you change your syntax to N1(String s1), then you will get the answer -dabc.

HTH
Phalguni
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on constructor