Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on constructor

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic