Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Can you call a constructor within a constructor?

Answers were Sorted based on User's Feedback



Can you call a constructor within a constructor?..

Answer / ranganathkini

Yes once constructor can call another constructor by using
the this() statement. Example:

class Account {
private double balance;

// 1st Constructor
public Account( double initBalance ) {
balance = initBalance;
}

// 2nd Constructor
public Account() {
this( 0.0 ); // call the 1st constructor
}
}

Is This Answer Correct ?    8 Yes 1 No

Can you call a constructor within a constructor?..

Answer / nikhlesh gupta datia(m.p.)

We can call one of base class constructor inside derive class constructor by using predefine variable "super()" such as super(a), super(a,b) and so on.

Is This Answer Correct ?    3 Yes 0 No

Can you call a constructor within a constructor?..

Answer / qim2010

Yes, by using this() syntax. E.g.

public Pet(int id) {
this.id = id; // “this” means this object
}
public Pet (int id, String type) {
this(id); // calls constructor public Pet(int id)
this.type = type; // ”this” means this object
}

Is This Answer Correct ?    1 Yes 0 No

Can you call a constructor within a constructor?..

Answer / harpreet

And to call the constructor of some other class, use super()

Is This Answer Correct ?    2 Yes 2 No

Can you call a constructor within a constructor?..

Answer / jinendra pandey

when i am tring above code getting following error
Can we call a constructor within a constructor?

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function


#include<iostream.h>
class Account {
private:
double balance;

// 1st Constructor
public:
Account( double initBalance ) {
balance = initBalance;
}

// 2nd Constructor
public:
Account() {
this( 0.0 ); // call the 1st constructor
}
};

int main()
{
Account a;
return 0;
}

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function

Is This Answer Correct ?    1 Yes 2 No

Can you call a constructor within a constructor?..

Answer / ravikiran(aptech mumbai)

yes we can call by make use of this()

Is This Answer Correct ?    0 Yes 1 No

Can you call a constructor within a constructor?..

Answer / amit kumar gupta

yes we can call by super() or this();

public abstract class Class1{

public Class1(){
System.out.println("Class1");
}
}

public class Class2 extends Class1{

public Class2(String Name){
System.out.println("Name = " + Name);
}

public Class2(){
this("AMIT"); // use only one this or super
super();
}
}

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

Program to find greatest prime number in n numbers?

3 Answers   Huawei,


How to split a string in java?

0 Answers  


In a container there are 5 components. I want to display all the component names, how will you do that?

0 Answers  


Difference between class#getinstance() and new operator ?

0 Answers  


What are the advantages of packages in java?

5 Answers  


What is ResourceBundle class?

1 Answers  


What is java used for?

0 Answers  


What invokes a thread's run() method in java programming?

0 Answers  


What is static import in java?

0 Answers  


Why spring singleton is not thread safe?

0 Answers  


What is the Vector class?

3 Answers  


what are the boundaries of jdk and jre?

0 Answers   CoreObjects,


Categories