Various types of Constructor in Java with example

Constructor in Java

Constructor in Java, constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type. There are different types of constructors in Java:

  • Default Constructor
  • Parameterized Constructor
  • No-argument Constructor
  • Copy Constructor

Let’s discuss each type with examples:

Default Constructor

A default constructor is provided by the compiler if no constructor is explicitly defined in the class. It initializes objects with default values.

class Dog {
    String name;
    int age;

    // Default constructor
    Dog() {
        System.out.println("Default Constructor called");
    }

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Dog dog1 = new Dog();
        dog1.display();
    }
}

Parameterized Constructor in Java

A parameterized constructor is defined by the programmer and can take arguments to initialize the object with specific values.

class Cat {
    String name;
    int age;

    // Parameterized constructor
    Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Cat cat1 = new Cat("Whiskers", 3);
        cat1.display();
    }
}

No-argument Constructor in Java

A no-argument constructor is a constructor that does not take any arguments. It can be defined explicitly by the programmer.

class Bird {
    String name;
    int age;

    // No-argument constructor
    Bird() {
        name = "Sparrow";
        age = 2;
    }

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Bird bird1 = new Bird();
        bird1.display();
    }
}

Copy Constructor in Java

A copy constructor initializes an object using another object of the same class. Java does not provide a default copy constructor, so it must be defined by the programmer.

class Rabbit {
    String name;
    int age;

    // Parameterized constructor
    Rabbit(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Copy constructor
    Rabbit(Rabbit rabbit) {
        this.name = rabbit.name;
        this.age = rabbit.age;
    }

    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Rabbit rabbit1 = new Rabbit("Thumper", 4);
        Rabbit rabbit2 = new Rabbit(rabbit1); // Using the copy constructor
        rabbit2.display();
    }
}

Summary

  • Default Constructor: Provided by the compiler, initializes with default values.
  • Parameterized Constructor: Defined by the programmer, takes arguments to initialize.
  • No-argument Constructor: Explicitly defined by the programmer, takes no arguments.
  • Copy Constructor: Defined by the programmer, initializes an object using another object.

Each type of constructor serves different purposes and allows for flexible object initialization in Java.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *