• 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

Constructors

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
can anyone tell how the hierarchy of constructors are fired.

class A{
A(){}
A(int i){
this();
System.out.println(" int A");
}
}
class B extends A{
public boolean B(String s) {
System.out.println("B string");
return false ;
}
}
class Test extends B{
private Test(){
super();
System.out.println("test");
}

public Test(String a){this() ;
System.out.println("string test");
}
public Test(int i){
System.out.println("int test");
}
public static void main(String args[]){
new Test(3) ; // here
}
}

at the commented line .... I think the compiler should give an error telling that the parent B has no constructor with an int.... or there is no default constructor ...mean B(). but this does'nt happen...the code compiles fine.
Can anyone explain.
Thx in advance
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aru:
Hi All,
can anyone tell how the hierarchy of constructors are fired.

class A{
A(){}
A(int i){
this();
System.out.println(" int A");
}
}
class B extends A{
public boolean B(String s) {
System.out.println("B string");
return false ;
}
}
class Test extends B{
private Test(){
super();
System.out.println("test");
}

public Test(String a){this() ;
System.out.println("string test");
}
public Test(int i){
System.out.println("int test");
}
public static void main(String args[]){
new Test(3) ; // here
}
}

at the commented line .... I think the compiler should give an error telling that the parent B has no constructor with an int.... or there is no default constructor ...mean B(). but this does'nt happen...the code compiles fine.
Can anyone explain.
Thx in advance


The compiler is providing that default constructor B() for B class.
public boolean B(String s) {
System.out.println("B string");
return false ;
} This is not a constructor. It has a return type. This method is just another method.
Savithri
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi aru,

just check up your program once.In class B you haven't define any constructors particularly so automatically default constructor will be provided so you won't get the error you are expecting..anyone please correct me if anything wrong in my explanation..
Thanks
krishna
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx all ......i got it.....was confused.
 
reply
    Bookmark Topic Watch Topic
  • New Topic