• Post Reply Bookmark Topic Watch Topic
  • New Topic

two constructors  RSS feed

 
xiaoxiao chou
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I define two constructors in the same class

i.e.
public class Car{
double fuelConsumption,tankSize,fuelInTank;

public Car(double fuelConsumption,double tankSize, double fuelInTank,Locat\
ion location){
this.fuelConsumption=fuelConsumption;
this.tankSize=tankSize;
this.fuelInTank=fuelInTank;
this.location=location;
}
public Car(double fuelConsumption,double tankSize,Location location){
this.fuelConsumption=fuelConsumption;
this.tankSize=tankSize;
this.location=location;
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The answer to your question is "yes". Note that you can define one in terms of the other, using less code and ensuring that all the constructors work the same way:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!