• 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

constructor question

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,
again, i stomped agains this question in a mock exam

public class Test20 extends A{
Test20(){
this("Hi");
}
Test20(String str){
System.out.println(str);
}
public static void main(String args[]) {
Test20 t = new Test20();
}
}
class A{
A(){
System.out.println("Super");
}
}
What will be the output?
A1 Super Hi
A2 Hi Super
A3 Super
A4 Compiler Error



result was A1, but i thought that super() and this() cannot be called at the same time....... it's either or as far as i understood.....
or is it only regarding explicit super() and explicit this() calls?

anyone could clarify?

thanks and regaqrds
marco
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test20() calls this() but the super() is called in Test20(String str).

So before the call to System.out.println(str) in the Test20(String str) constructor there is a compiler supplied call to super() that calles A().
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test20 extends A{
Test20(){
this("Hi");
}
Test20(String str){
System.out.println(str);
}
public static void main(String args[]) {
Test20 t = new Test20();
}
}
class A{
A(){
System.out.println("Super");
}
}
What will be the output?
A1 Super Hi
A2 Hi Super
A3 Super
A4 Compiler Error

Hi!
It's a good question!

result was A1, you thought that super() and this() cannot be called at the same time....... All right !!but you must look at this()in constructor! It's not called super(). The output is show that class A is superclass ,Test20 is noly a subclass !when you write
Test20(){
//this("Hi");
}
The answer is super! I just think default call super(). Maybe my idea is not correct!but I deem that!
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you cannot call super() and this() at the same time. From within a constructor, have a look at this...

super();
this(1);

This violates the rule that super() or this() needs to be at line 1. Thats what it means.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test20(){
this("Hi");
}
Test20(String str){
System.out.println(str);
}


You are right ,first thing you have to understand is if there is no this or super in the first line of constructor compiler automatically inserts "super()",
in Test20 constructor this(String) is inserted so compiler will not insert super() but in Test20(String)constructor there is no this or super in first line so compiler inserts automatically super() call.

so Test() calls Test(String),this in turn calls Super class constructor since super is inserted.

Hope it is clear.
 
reply
    Bookmark Topic Watch Topic
  • New Topic