It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sample experimanting mock code(consturctor related) 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 "sample experimanting mock code(consturctor related)" Watch "sample experimanting mock code(consturctor related)" New topic
Author

sample experimanting mock code(consturctor related)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


above code compiles fine.
we know if we are invoking the subclass overloaded constructor,then super-class no arg constructor will be
invoked first, and if super-class no arg constructor is not there, then it will be a compiler error.
if we commented out line2 ,we have to defined superclass no arg constructor(i.e. uncommented out line 1), othervise it
gives a compiler error,what is the logic behind it?

why we don't need no arg. super-class constructor(default) here ,as we are instantiating subclass object(with arg.)?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
Arjun

within a constructor the first line should be either a call to this() or super()

and when the compiler doesnt find such a call, it inserts super() by default.

in your code the Main() constructor already has a call to super(arg) and thus super() will not be invoked.
try removing the super(arg) line and you will see that the code will not compile.

Refer to K&B for detailed description about this.(very nicely explained in that book)


OCPJP 6-100%; Preparing for GATE11
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

yeah,thanks garima for clearing my query
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
Happy to help
 
Don't get me started about those stupid light bulbs.
 
subject: sample experimanting mock code(consturctor related)