aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this(),super() 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 "this(),super()" Watch "this(),super()" New topic
Author

this(),super()

Anupama Kota
Greenhorn

Joined: May 21, 2001
Posts: 22
public class Myclass
{
public static void main(String args[])
{
B b = new B("Test");
}
}
Class A
{
A()
{
this("1","2");
}
A(String s,String t)
{
this(s+t);
}
A(String s)
{
System.out.println(s);
}
}
class B extends A
{
B(String s)
{
System.out.println(s);
}
B( String s,String t)
{
this(s+t+"3");
}
B()
{
super("4");
}
}
In text it gives output 12 followed by Test.But i cant understand why it is printing like that???Pl clear me.
Amit Barot
Greenhorn

Joined: May 29, 2001
Posts: 13
Hi
i think u are reffering Java Certification book by Khalid Mughal

Whenever u instantiate a class the its default constructor is implicitly invoked in this case class B extends A so by rule
the default constructor of A will also be invoked implicitly
the default constructor call other constructor this("1","2")
and that constructor in turn call other constructor after that the flow returns in the reverse order ie from the Superclass to the subclass and ultimately to the class B where it gets printed
hope this solves your dilemma.
bye cya
Anupama Kota
Greenhorn

Joined: May 21, 2001
Posts: 22
Hi
Amith
B has its own constructor explicitly na .so why it call A constructor implicitly.i am confusing in this point only .pl clear me.
Li Yi
Greenhorn

Joined: Jun 21, 2001
Posts: 8
B b = new B("Test");
This causes class B to be initialized, but before that, it needs to initialize B's direct superclass A, since no constructor was called in A, it uses the default: A(), which in turn calls constructor A(String s,String t) because of the signature used, which in turn calls another constructor A(String s) because of the signature, which then prints "12". when it finally back to class B, constructor B(String s) is called also because of signature, which then prints "test".
According to java language specification: "Before a class is initialized, its direct superclass must be initialized"
[This message has been edited by Li Yi (edited June 22, 2001).]
Haining Mu
Ranch Hand

Joined: Jun 01, 2001
Posts: 51
the B's constructor you call is B(String s), the compiler will insert a "super();" if the first line is neither this(...) nor super(...). So A() is called.
[This message has been edited by Haining Mu (edited June 22, 2001).]
Amit Barot
Greenhorn

Joined: May 29, 2001
Posts: 13
Hi again
C its a rule in java that whenever u instantiate a class
Its default cosntructor is called automatically.
And provided that if it extends some class then automaticaaly
the first line executes is super() that is the call to the default constructor of the super class in this case it is A
and after that it is chained by this.
Hope u get it now
If u hav any more problems then feel free to ask me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this(),super()