• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on constructor

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic