Java Constructors (Part 2)

Pre-requisites:-  Method Overloading , Inheritance 

Copy Constructor 

Copy Constructor is a constructor which creates an object by initializing it to the object of the same class which is previously declared. 

Writing a Copy Constructor

First a full parameterized constructor is declared so a copy constructor can copy its attributes.

Then an object of that constructor is passed as an argument in copy constructor.

While creating object of parameterized constructor , the same object is passed while instantiating copy constructor. And this will generate copy constructor.

Eg:-
 import java.util.Scanner;  
   
 public class MyClass {  
      private String name;  
      private int id;  
   
      // normal parameterized constructor  
      MyClass(String name, int id) {  
           this.name = name;  
           this.id = id;  
      }  
   
      // copy constructor , passing here object 'obj' of normal const.  
      MyClass(MyClass obj) {  
           this.name = obj.name;  
           this.id = obj.id;  
      }  
   
      // normal method  
      public void getInfo() {  
           System.out.println("Name is: " + name);  
           System.out.println("Id is: " + id);  
      }  
   
      public static void main(String[] args) {  
           Scanner scan = new Scanner(System.in);  
           System.out.println("Enter your name:");  
           String name = scan.nextLine();  
           System.out.println("Enter your Id:");  
           int id = scan.nextInt();  
   
           // calling normal constructor  
           MyClass obj = new MyClass(name, id);  
           System.out.println("---------------");  
           System.out.println("Original Details: ");  
           obj.getInfo();  
   
           // calling copy constructor i.e passing object 'obj' of normal const.  
           MyClass copyObj = new MyClass(obj);  
           System.out.println("---------------");  
           System.out.println("Copy of Details: ");  
           copyObj.getInfo();  
           scan.close();  
      }  
 }  
Output:-
 Enter your name:  
 Bob  
 Enter your Id:  
 123  
 ---------------  
 Original Details:   
 Name is: Bob  
 Id is: 123  
 ---------------  
 Copy of Details:   
 Name is: Bob  
 Id is: 123  
 

Constructor Overloading

In Java , Constructors are methods without return types , also they can be overloaded too.Constructor Overloading is a technique of having more than one constructor with different parameter lists. 

They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.


Eg:-
 public class MyClass {  
      private String name;  
      private int age;  
      private int id;  
   
      // normal parameterized constructor  
      MyClass(String name, int age) {  
           this.name = name;  
           this.age = age;  
      }  
   
      // constructor overloaded with int id  
      MyClass(String name, int age, int id) {  
           this.name = name;  
           this.age = age;  
           this.id = id;  
      }  
   
      // normal method  
      void getInfo() {  
           System.out.println("Name is: " + name);  
           System.out.println("Age is: " + age);  
           System.out.println("Id is: " + id);  
      }  
   
      public static void main(String[] args) {  
   
           // calling constructor with two parameters  
           MyClass obj1 = new MyClass("Bob", 18);  
           obj1.getInfo();  
   
           System.out.println("---------------");  
   
           // calling constructor with three parameters  
           MyClass obj2 = new MyClass("John", 19, 12345);  
           obj2.getInfo();  
      }  
 }  
Output:-
 Name is: Bob  
 Age is: 18  
 Id is: 0  
 ---------------  
 Name is: John  
 Age is: 19  
 Id is: 12345  
   


Constructor Chaining

  • Constructor Chaining is process of calling one constructor from another.Constructor chaining occurs through Inheritance.
  • This ensures that creation of child class object starts with the initialization of the data members of the Parent class. 
  • There can be any numbers of classes in inheritance chain. Every constructor calls up the chain constructor till constructor at the top is reached and starts execution from there.



1. Constructor Chaining using this( ) keyword

Here chaining is done in the same class i.e constructor chaining inside a class.

Eg:-
 public class MyClass {  
      MyClass() {  
           System.out.println("Chain Starting");  
           System.out.println("1st constructor");  
      }  
      MyClass(int age) {  
           this();  
           System.out.println("2nd constructor");  
      }  
      MyClass(String name, int age) {  
           this(19);  
           System.out.println("3rd constructor");  
           System.out.println("Chain finished");  
      }  
      public static void main(String[] args) {  
           new MyClass("Bob", 19);  
      }  
 }  
Output:-
 Chain Starting  
 1st constructor  
 2nd constructor  
 3rd constructor  
 Chain finished  


2. Constructor Chaining using Inheritance

Here constructor chaining is between classes , also classes are inherited i.e parent-child relationship 

Eg:-
 class Parent {  
      Parent() {  
           System.out.println("Parent Constructor");  
      }  
 }  
 class Child1 extends Parent {  
      Child1() {  
           System.out.println("Child1 Constructor");  
      }  
 }  
 class Child2 extends Child1 {  
      Child2() {  
           System.out.println("Child2 Constructor");  
      }  
 }  
 public class MyClass {  
      public static void main(String args[]) {  
         //it will invoke both parents constructor i.e Parent & Child1 
          new Child2();  
      }  
 }  
Output:-
 Parent Constructor  
 Child1 Constructor  
 Child2 Constructor  



< Previous Next >