Question:
what is the first statement in construtor?
Answer:
The first statement of the constructor should be either a call to super() or this(). ex. class A{ A(){ super(); / this(); } } Source: CoolInterview.com
Answered by: Avishek Kar | Date: 9/25/2007
| Contact Avishek Kar
There is nothing such like a first line in constructor.The constructor can include any initializations to be performed depending upon the parameters passed.The constructor may even contain no code at all.
For example,
public Bank() { }
This constructor initializes a class Bank for no default values.This is called a empty constructor.
public Bank(double interest) { interestRate=interest; }
This constructor intializes the interest rate for the Bank Class. Source: CoolInterview.com
Answered by: Amit Rane | Date: 9/26/2007
| Contact Amit Rane
super(); Source: CoolInterview.com
Answered by: Swetha | Date: 11/12/2007
| Contact Swetha
There is certainly first statemnt in the Constructor which should be Optionally Provided by You.
and It depends on mainly 2 Factors
1 Parent Class Exist for your base Class 2 you want bypass to some other Constructor in Your Base Class
Let me Explain the First Case Having Parent class
Class SuperClass{ SuperClass(){} SuperClass(int i,flaot b) }
Class BaseClass extends SuperClass{
// say you have Constructor of No Arg then you will write
BaseClass(){ } //In the Above Case if not super is Provided by you JVM will provide one for you and looks as below
BaseClass(){ super(); }
//and if you want to have argumented Consrtuctor then mandatorly you should place super() with argumnets
Baseclass(int a,float b){ super(a,b); }
// now if you want call overloaded Constructor then you can use this() BaseClass(int a,int b){ this(a,new Float(b)) }
} Source: CoolInterview.com
Answered by: Praveen | Date: 4/25/2008
| Contact Praveen
the first statement of the constractors should be super or this or any local variable alse can be wrritten in the constractors. Source: CoolInterview.com
Answered by: rusho | Date: 8/9/2008
| Contact rusho
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|