-3

I come from JS.

In order to do some native development, I started learning Java.

In there they have an example like this to explain different constructors (mainly normal constructor, constructor overloading and copy constructor).

class Pet {
    private int petAge;
    private String petType;
    private String petName;

    //This is the constructor without any paramters
    public Pet() {
        petAge = 0;
        petName = "";
        petType = "";
    }

    //This is the constructor with parameters
    public Pet(String name, String type, int age) {
        petAge = age;
        petType = type;
        petName = name;
    }

    //This is the copy constructor
    public Pet(Pet copyThisPet) {
        petName = copyThisPet.petName;
        petType = copyThisPet.petType;
        petAge = copyThisPet.petAge;
    }

    public void print() {
        System.out.println("Pet Name: " + petName);
        System.out.println("Pet Type: " + petType);
        System.out.println("Pet Age: " + petAge);
    }
}

class pet_list {
    public static void main(String[] args) {
        Pet dog = new Pet();
        dog.print();

        Pet cat = new Pet("Princess", "cat", 45);
        cat.print();

        Pet cat_copy = new Pet(cat);
        cat_copy.print();
    }
}

I get the example but I haven't got how Java decides which constructor to invoke? Also, what if there are two constructors which take string as input?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Rahul Patel
  • 63
  • 1
  • 11
  • 2
    `new Pet()` passes no arguments, so it calls the no-argument constructor. `new Pet("Princess", "cat", 45)` passes two strings and an int so it calls the `(String, String, int)` constructor. `new Pet(cat)` passes a pet so it calls the `(Pet)` constructor. What part of it are you uncertain about? – khelwood May 15 '20 at 13:14
  • See [What is overloading in Java?](https://stackoverflow.com/questions/13123180/what-is-overloading-in-java) – khelwood May 15 '20 at 13:17
  • @khelwood What is there are two constructors which accept string as input and perform separate task on inti ` public Pet(String name) {// Do task A }` and `public Pet(String name) {// Do task B}` which is `java` going to pick on initialisation? ` Pet dog = new Pet('dog');`? – Rahul Patel May 15 '20 at 13:19
  • 3
    Then your program would not compile. – khelwood May 15 '20 at 13:21
  • 1
    @RahulPatel Just *try* it—you’d find out immediately. – Dave Newton May 15 '20 at 13:28
  • By the way, if your question was about multiple constructors with similar arguments, why post a load of code that does not illustrate that at all? – khelwood May 15 '20 at 13:32

2 Answers2

2

Java choose the proper constructor based on the parameters contained in the constructor invocation. The same logic applies to methods.

what if there are two constructor which take string as input?

This is not possible: you can't have multiple constructors/methods with the same signature.

Couper
  • 414
  • 5
  • 13
0

You cannot have two functions/constructors inside a class with same number and type of arguments in Java.

In case you have two constructors which take string as input then you will get an error because one constructor which takes string as input will be able to do the job you want to do you wouldn't need second constructor to do the same job.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
javaNoob
  • 86
  • 1
  • 9