Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors from exam cram

 
Krishna Priya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Priya
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Varsha Dighe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane,
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 ?
-Varsha
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3752
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help you :
http://enthuware.com/jqplus/FAJQuestions.html#DEFAULTCONSTRUCTOR
and
http://enthuware.com/jqplus/FAJQuestions.html#SUPERCONSTRUCTOR
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern) www.enthuware.com/jqplus
[This message has been edited by Paul Anil (edited October 21, 2000).]
 
vijay krishna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varsha,
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Paul and Vijay. I understood the concept now.
Varsha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic