wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doesn't an Inner Class constructor propagate upwards by adding 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 "Doesn Watch "Doesn New topic
Author

Doesn't an Inner Class constructor propagate upwards by adding super() ?

Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22


The output is super super sub inner

When Line 12 is executed, both the instance init blocks are executed after insertion of super().
So first MySub is instantiated on line 10 which inserts a super() call to MySuper() and hence super is printed followed by sub being printed. This propagation happens for normal class constructors. But finally when MyInner() is intantiated why isn't super() called for MySub and subsequently to MySuper()? Why isn't there a down to top propagation in the hierarchy? Isn't MyInner technically a subclass of MySub?

I hope my question is clear
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Rahul Choudhary wrote:But finally when MyInner() is intantiated why isn't super() called for MySub and subsequently to MySuper()? Why isn't there a down to top propagation in the hierarchy? Isn't MyInner technically a subclass of MySub?


No, MyInner isn't a subclass of MySub. Each MyInner is contained within a MySub, but there's no inheritance unless you add extends MySub.

So the calls propagate as usual, but it's only the Object constructor that gets called.
Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
Great! Thanks Matthew
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
How the output is printed:
1. new MySuper () is called, it calls super() (the constructor of Object) and prints "super".
2. new MyInner() is called, it calls super() (the constructor of Object) and calls new MySub() (the initialization block).
3. In the constructor of MySub(), it calls super() (the constructor of MySuper and MySuper calls its super() as well.). It prints "super"
4. Next, in the MySub(), after calling super(), it prints "sub".
5. Go back to MyInner() contructor, it finishes its super() call and prints "inner".

Modify the code like this and you will see how it works:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doesn't an Inner Class constructor propagate upwards by adding super() ?