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
Joined: Aug 30, 2000
Posts: 3141
posted
0
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
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
------------------ 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).]
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
Joined: Oct 14, 2000
Posts: 32
posted
0
Thank you very much Paul and Vijay. I understood the concept now. Varsha