This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I tried to run the code it gives compiler error for Super() constructor.
When I added no-args constructors for both the classes it worked fine, I don't get the reason behind the error.
Can anyone please help me ?
Thanks,
- Prajakta
Gaurav Bhatia
Ranch Hand
Joined: Jan 01, 2007
Posts: 49
posted
0
Whenever a call to derived class constructor is made, it further calls the constructor of base class. If there is no constructor in base class then it will insert a no argument constructor. But in case there exists a parameterized constructor in base then then it should be specifically called using .
In your case the base class contains a constructor and there is no call to this constructor from the constructor of derived class. So, it failed.
Hope this clarifies.
~Gaurav<br />SCJP5
shankar reddy
Ranch Hand
Joined: Jun 04, 2007
Posts: 71
posted
0
Hi, Is there is any argumented constructor in the super class , the subclass has to call the superclass constructor,voilation leads to compilation error.
In general what happens is ..
class SuperClass { SuperClass() { } } class SubClass extends SuperClass { SubClass() { } } After compilation the generated code will be like this..
class SuperClass { SuperClass() { super(); } } class SubClass extends SuperClass { SubClass() { super(); } }
here when you are creating object for SubClass ,the constructor in the SubClass will call the super class construtor. If you understand this you can get the answer easily. see the following code.. class SuperClass { SuperClass(int a) { } } class SubClass extends SuperClass { SubClass() { } } Here ,the generated code is...
class SuperClass { SuperClass(int a) { super(); } } class SubClass extends SuperClass { SubClass() { super(); // Here is the problem ,the control where to go ..no way // to rectifiy the problem, We have to give manually // like this for ex:- super(12); } }
Hence we are the responsibilty for to call the super class construtor ,using super(parameters);
I hope you understood .. give me your valuable FEEDBACK.
Hi, Is there is any argumented constructor in the super class , the subclass has to call the superclass constructor,voilation leads to compilation error.
In general what happens is ..
class SuperClass { SuperClass() { } } class SubClass extends SuperClass { SubClass() { } } After compilation the generated code will be like this..
class SuperClass { SuperClass() { super(); } } class SubClass extends SuperClass { SubClass() { super(); } }
here when you are creating object for SubClass ,the constructor in the SubClass will call the super class construtor. If you understand this you can get the answer easily. see the following code.. class SuperClass { SuperClass(int a) { } } class SubClass extends SuperClass { SubClass() { } } Here ,the generated code is...
class SuperClass { SuperClass(int a) { super(); } } class SubClass extends SuperClass { SubClass() { super(); // Here is the problem ,the control where to go ..no way // to rectifiy the problem, We have to give manually // like this for ex:- super(12); } }
Hence we are the responsibilty for to call the super class construtor ,using super(parameters);
I hope you understood .. give me your valuable FEEDBACK.
Mary John
Ranch Hand
Joined: Sep 17, 2007
Posts: 108
posted
0
When your super class has a constructor defined with arguments then you should explicitly call that super constructor in the subclass constructor's first line and give the required arguments to the super constructor.
Since your superclass has a constructor with arguments defined, Compiler will not provide a default no-arg constructor in your superclass.It will only put to a default call to super() fromyour subclass constructor which currently you wont have.
Hope its clear
SCJP 5.0<br />SCJD working on it
Prajakta Vaidya
Greenhorn
Joined: Oct 10, 2007
Posts: 10
posted
0
Thnx
Actually I thought only from the no-args constructor of the subclass the super() gets called, and in this case no object of Sub class is created like Sub sb = new Sub(), compiler won't complain
Divya Gehlot
Ranch Hand
Joined: Sep 10, 2006
Posts: 238
posted
0
HiPrajakta, For your code just provide consructor for Super class your code will compile and run becuse of the Constructor Rules you have to do that. Just remember the rules for constructor it will be easier for you debug the code. below is the rule for your code --- Rules For Constructor [ If you want a no-arg constructor and you've typed any other constructor(s) into your class code, the compiler won't provide the no-arg constructor (or any other constructor) for you. In other words, if you've typed in a constructor with arguments, you won't have a no-arg constructor unless you type it in yourself ! Hope your doubt got clear now... [ October 10, 2007: Message edited by: Divya Gehlot ]
SCJP1.5(81%)<br />SCDJWS(94%)<br />next mission SCEA(but need to wait or that)
shankar reddy
Ranch Hand
Joined: Jun 04, 2007
Posts: 71
posted
0
Hi Prajakta Vaidya and All JAVA Lovers, I request you that please reply with feedbacks. Because most of the members are answering the questions with lot of patience. If there are mistakes in the way of presentation they will rectify.