File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructors from exam cram 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 "constructors from exam cram" Watch "constructors from exam cram" New topic

constructors from exam cram

Krishna Priya

Joined: Jun 08, 2000
Posts: 5
Hi All
Could anyone of you please give me the correct answer for the folowing code:
Given the following class definition:
1. public class deriveddemo extends demo {
2. int M,N,L;
3. public deriveddemo(int x,int y) {
4. M=x;N=y;
5. }
6.public deriveddemo(int x) {
7. super(x);
8. }
9. }

which of the following constructor signatures must exist
in the demo class for deriveddemo to compile correctly?
a.public Demo(int a,int b)
b.public Demo(int c)
c.public Demo()
I think the correct answer is only b.But the correct answer
is b and c.Howcome c is correct ans?
Thanks and Regards
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Krishna,
When you create an object the Runtime goes through a series of initialization steps. One of which is to invoke super(). If the base class does not have a default constructor the initialization fails. (see JLS§12.5).
Hope that helps.

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
Does that mean one has to have a explicitely declared constructor (with no arguments) defined in a class, if we wish to extend the class ?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
This may help you :

Get Certified, Guaranteed!
(Now Revised for the new Pattern)
[This message has been edited by Paul Anil (edited October 21, 2000).]

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
vijay krishna

Joined: Oct 17, 2000
Posts: 8
Any constructor in sub class 1st calls default constructor super() (if there is no "super" or "this" specified).
If there is such a constructor in SUB CLASS then "Yes", you have to have one default constructor in super class.
OTHERWISE You dont have to have default constructor in super calss.
Is that clear?
Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
Thank you very much Paul and Vijay. I understood the concept now.
I agree. Here's the link:
subject: constructors from exam cram
It's not a secret anymore!