File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sample experimanting mock code(consturctor related)
 
Similar Threads
Please clarify this regarding super class construcors
Can you please clarify my doubt ?
why not give a no-arg constructor
constructor giving headache
Superclass that requires any of its subclasses to create a no-arg constructor?