Constructors

Constructors:
  • It is special type of method which does not have return type
  • Main use of constructor is to initialize the data members
  • It is used to create a object in different ways
  • Method name is same as class name

Types of Constructor:
  1. Default Constructor
  2. Zero-Argument Constructor
  3. Parameterized Constructor
1. Default Constructor:
    If no constructor is defined inside the class then compiler will create a default constructor which is used to initialize data members with default values.

package com.core.java;

public class Constructors {
    byte b; short s; int i; long l;
    float f; double d; boolean bool;
    char ch; String str;
    public static void main(String[] args) {
        Constructors cons = new Constructors();
        System.out.println("Default value of byte: " + cons.b);       //0
        System.out.println("Default value of short: " + cons.s);      //0
        System.out.println("Default value of int: " + cons.i);        //0
        System.out.println("Default value of long: " + cons.l);       //0
        System.out.println("Default value of float: " + cons.f);      //0.0
        System.out.println("Default value of double: " + cons.d);     //0.0
        System.out.println("Default value of char: " + cons.ch);      //U+0000
        System.out.println("Default value of boolean: " + cons.bool); //false
        System.out.println("Default value of String: " + cons.str);   //null
    }
}

2. Zero-Argument Constructor:
    Constructor with zero parameters is called Zero-Argument Constructor. It is used to initialize with your own default value.

package com.java;

public class Employee {
    int id;
    String name;
    double salary;
    String city;
    
    public Employee(){
        id = 111;
        name = "Tom";
        salary = 15000;
        city = "Bengaluru";
    }
    public static void main(String[] args) {
        Employee e = new Employee();
        System.out.println("Id: " + e.id);            //id: 111
        System.out.println("Name: " + e.name);        //Name: Tom
        System.out.println("Salary: " + e.salary);    //Salary: 15000.0
        System.out.println("City: " + e.city);        //City: Bengaluru
    }
}

3. Parameterized Construstor:
    Constructor with parameters is called Parameterized Constructor.

package com.java;

public class Employee {
    int id;
    String name;
    double salary;
    String city;
    
    public Employee(int i, String n, double sal, String c){
        id = i;
        name = n;
        salary = sal;
        city = c;
    }
    public static void main(String[] args) {
        Employee e = new Employee(222, "Michel", 25000, "Hyderabad");
        System.out.println("Id: " + e.id);            //id: 222
        System.out.println("Name: " + e.name);        //Name: Michel
        System.out.println("Salary: " + e.salary);    //Salary: 25000.0
        System.out.println("City: " + e.city);        //City: Hyderabad
    }
}

Constructor Overloading:
    class can have multiple constructor with same name as class name but different parameters is called Constructor Overloading.

package com.java;

public class Employee {
    int id;
    String name;
    double salary;
    String city;
    
    //Zero-Parameterized Constructor
    public Employee(){ 
        id = 111; 
        name = "Tom"
        salary = 15000; 
        city = "Bengaluru"
    }
    
    //1-Parameterized Constructor
    public Employee(int id){
        this();
        this.id = id;
    }
    
    //2-Parameterized Constructor
    public Employee(int id, String name){
        this(id);
        this.name = name;
    }
    
    //3-Parameterized Constructor
    public Employee(int id, String name, double salary){
        this(id, name);
        this.salary = salary;
    }
    
    //4-Parameterized Constructor
    public Employee(int id, String name, double salary, String city){
        this(id, name, salary);
        this.city = city;
    }
    
    public String toString(){
        return "Id: " + id + ", Name: " + name + ", Salary: " + salary + ", city: " + city;
    }
    public static void main(String[] args) {
        Employee e0 = new Employee();
        System.out.println(e0);    //Id: 111, Name: Tom, Salary: 15000.0, city: Bengaluru
        
        Employee e1 = new Employee(222);
        System.out.println(e1); //Id: 222, Name: Tom, Salary: 15000.0, city: Bengaluru
        
        Employee e2 = new Employee(222, "Michel");
        System.out.println(e2); //Id: 222, Name: Michel, Salary: 15000.0, city: Bengaluru
        
        Employee e3 = new Employee(222, "Michel", 25000);
        System.out.println(e3); //Id: 222, Name: Michel, Salary: 25000.0, city: Bengaluru
        
        Employee e4 = new Employee(222, "Michel", 25000, "Hyderabad");
        System.out.println(e4); //Id: 222, Name: Michel, Salary: 25000.0, city: Hyderabad
    }
}

Singleton Design Pattern:
It is pattern where Constructor is private and static since you can create only one Object.


package com.core.java;

class Singleton {
    public int id;
    public String name;
    public double d;
    //private static Singleton obj = new Singleton();
    private static Singleton obj;

    private Singleton(){
        System.out.println("Object is created");
    }
    public static Singleton getObject() {
        if (obj == null) {
            obj = new Singleton();
        }
        return obj;
    }
    public void display() {
        System.out.println(id + ", " + name + ", " + d);
    }
}
public class Test {
    public static void main(String[] args) {
        //Singleton obj = new Singleton(); //Compiler Error (Cannot create Object because constructor is private)
        Singleton obj1 = Singleton.getObject();
        obj1.id = 111; 
        obj1.name = "\"Singleton Design Pattern\"";
        obj1.d = 15.56;
        obj1.display();
        Singleton obj2 = Singleton.getObject();
        obj2.display();
        Singleton obj3 = Singleton.getObject();
        obj3.display();
        Singleton obj4 = Singleton.getObject();
        obj4.display();
    }
}

No comments:

Post a Comment