Multiple constructors

 
/**
 * Constructor use the class name and has no return type.
 * Every class you create has a default constructor, even if you don't write one.
 * You could have more than one constructor in a class.
 */

package com.minte9.oop.constructors;

public class MultipleConstructors {
    public static void main(String[] args) {

        new A();  // Default constructor used
        new A("aaa");  // Constructor 1 - aaa
        new A(100);  // Constructor 2 - 100
    }
}

class A {

    // Default contructor (written explicitly for clarity)
    public A() {
        System.out.println("Default constructor used");
    }

    // Constructor 1
    public A(String s) {
        System.out.println("Constructor 1 - " + s);
    }

    // Constructor 2
    public A(int n) {
        System.out.println("Constructor 2 - " + n);
    }
}

This method

 
/**
 * To call another constructor from the same class use this() method.
 */

package com.minte9.oop.constructors;

public class ThisMethod {
    public static void main(String[] args) {
        
        Rectangle a = new Rectangle();
        Rectangle b = new Rectangle(100, 200);
        Rectangle c = new Rectangle(300, 400, 11, 22); // Look Here

        System.out.println(a);  // W: 0 H: 0 x: 0 y: 0
        System.out.println(b);  // W: 100 H: 200 x: 0 y: 0
        System.out.println(c);  // W: 300 H: 400 x: 11 y: 22
    }
    
}

class Rectangle {

    private int x, y;
    private int width, height;
    
    public Rectangle() {
        this(0, 0, 0, 0); // Look Here
    }
    
    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    
    public Rectangle(int width, int height, int x, int y) {
        this(width, height);
        this.x = x;
        this.y = y;
    }

    public String toString() {
        return String.format(
            "W: %s H: %s x: %s y: %s", width, height, x, y
        );
    }
}

Super Method

 
/**
 * The super() method is used to call parent constructor.
 * Constructor call must be the first statement in a controller.
 */

package com.minte9.oop.constructors;

public class SuperMethod {
    public static void main(String[] args) {
        new MainClass();  // Parent constructor called.
    }
}

class BaseClass {
    public BaseClass() {
        System.out.println("Parent constructor called.");
    }
}

class MainClass extends BaseClass {
    public MainClass() {   // Subclass controller
        super();  // Parent constructor called
    }

    /**
     * This constructor will not work because 'void' is the first statement.
     * public void MainClass(String s) {
     *      super(s);
     * }
     */
}






Questions and answers:
Clink on Option to Answer




1. What is true about a constructor in Java?

  • a) It uses the class name and has no return type
  • b) It must have a return type

2. What does it mean that a class can have multiple constructors?

  • a) Only one of them can ever be used
  • b) The class can have different parameter lists to create objects

3. What is the purpose of this() in a constructor?

  • a) To call another constructor in the same class
  • b) To call a method in the parent class

4. Why must this() be the first statement in a constructor?

  • a) Because it calls a method in the same class
  • b) Because constructor calls must happen first

5. What is the purpose of super() in a constructor?

  • a) To call the parent class constructor
  • b) To create a new object


References: