0

If a class contains two constructors that take in different types of arguments as shown here:

public class Planet {
   public double xxPos; //its current x position
   public double yyPos; //its current y position
   public double xxVel; //its current veolicity in the x direction
   public double yyVel; //its current veolicity in the y direction
   public double mass; //its mass
   public String imgFileName; //The name of an image in the images directory that depicts the planet

   // constructor is like __init__ from python, this sets up the object when called like: Planet(arguments)
   public Planet(double xP, double yP, double xV, double yV, double m, String img) {
      xxPos = xP;
      yyPos = yP;
      xxVel = xV;
      yyVel = yV;
      mass = m;
      imgFileName = img;

   }

   // second constructor
   // how come testplanetconstructor knows to use this second one?
   // does it know based on the argument type its being passed?
   public Planet(Planet p) {
      xxPos = p.xxPos;
      yyPos = p.yyPos;
      xxVel = p.xxVel;
      yyVel = p.yyVel;
      mass = p.mass;
      imgFileName = p.imgFileName;
   }
}

My primary question is: 1) How does another class with a main that calls this class determine which constructor to use?

If that is the case, what would happen if you have two constructors with: 2) the same type and number of arguments? 3) the same type but different number of arguments?

I realize that follow up questions are something that you should probably never do (aka something messy). I am just curious.

Kunseok
  • 263
  • 1
  • 7
  • The signature of a constructor or method are name + parameter types. These must be unique in a source (compiler error otherwise) and on usage the most fitting is used. – Joop Eggen Apr 28 '16 at 10:10
  • 1
    Your question seems to be asking how [overloading](https://en.wikipedia.org/wiki/Function_overloading) works. – khelwood Apr 28 '16 at 10:10
  • 1
    for (2) and (3), why don't you just run the code and see for your self ;) – Hoàng Long Apr 28 '16 at 10:12

3 Answers3

2

1) How does another class with a main that calls this class determine which constructor to use?

Compiler follows same process as overloaded method for static binding by checking unique method signature. To know about method signature see this

public static void main(String args[]) {
    double d1 = 0;
    double d2 = 0;
    double d3 = 0;
    double d4 = 0;
    double d5 = 0;
    String img = "";
    Planet p = new Planet(d1, d2, d3, d4, d5, img);// constructor with valid argument set
}

2) the same type and number of arguments?

This is actually not possible to write two method/constructor with same signature in a single class. e.g. following code never compile

Planet(int i) {// compilation error
    return 0;
}
Planet(int j) {// compilation error
    return 0;
}

3) the same type but different number of arguments?

This is possible, just like method creating / calling with different signature. e.g.

Planet p1 = new Planet(d1, d2, d3, d4, d5, img);
Planet p2 = new Planet(p1);
rev_dihazum
  • 818
  • 1
  • 9
  • 19
0

1) How does another class with a main that calls this class determine which constructor to use?

Classes don't determine anything, the programmer do, and he does it by placing the appropriate parameters. For example, if you have 2 constructors public Test (int i)and public Test(), when you call new Test(5) it will call the first, and if you do new Test() it will call the second.

What if..? 2) the same type and number of arguments?

You can't. It will not compile.

What if..? 3) the same type but different number of arguments?

The same that in 1)

dquijada
  • 1,697
  • 3
  • 14
  • 19
0

The constructor to use will be determined based on the number and type of arguments you will pass to it.

In Java, the constructor and method signatures (identities) are comprised of the method name (not applicable for constructors, obviously), and the number and type of its parameters.

Two methods (or constructors) cannot have the same signature. So you cannot provide two methods or constructors that would have the same number and type of parameters, which makes it easy for the JVM to know which one to call, just by matching the number and type of the actual parameters passed.

Olivier Croisier
  • 6,139
  • 25
  • 34