Ticker

6/recent/ticker-posts

JAVA Constructor | Constructor OverLoading

Constructor

  • Constructor is special kind of method which gets called when object is created 
  • Constructor don’t have return type 
  • Constructor cannot be inherited 
  • Constructor name is same has the class name 
  • Every class should have a constructor, if we don’t write a constructor compiler will write a default constructor. 
  • Compiler will not write a default constructor, if we are going to write any of the constructor explicitly 
  • Constructor with no parameters or a zero parameter constructor is called has default constructor 
  • Constructor with parameter is called has parameterized constructor. These constructor is used to initialise the instance variables at the time of creating object 

Syntax 

<accesses-specifier> ClassName ( ) 

        //some code… 

Eg :- class Cow 

        //zero parameter constructor of Cow Class 

        public Cow() 

        

                System.out.println("I am in Cow class constructor.....!"); 

        

Example:

class Cow 

            //zero parameter constructor of Cow Class 

            public Cow() 

            

                    System.out.println("I am in Cow class constructor.....!"); 

            

class Test1 

 { 

            public static void main(String[] args) 

         { 

                System.out.println("Before creating object"); 

                Cow ref = new Cow(); //when this line is executed the Cow() constructor is called

                System.out.println("After creating 1st object"); 

                new Cow(); // when this line is executed again the Cow() constructor is called 

                System.out.println("After creating 2nd object"); 

         } 

Output : 

Before creating object 

I am in Cow class constructor.....! 

After creating 1st object 

I am in Cow class constructor.....! 

After creating 2nd object 

Constructor Over Loading 

A class can contain more than one constructor inside it, but they should vary with number of parameters or type of parameters. This concept is called has contractor overloading. 

class Cow 

        public Cow() 

        

                System.out.println("I am in Cow() constructor.....!"); 

        

        public Cow(int leg) 

        

                System.out.println("I am in Cow(int leg) constructor.....!"); 

        

        public Cow(double height) 

        

                System.out.println("I am in Cow(double hight) constructor.....!"); 

        

        public Cow(int ears , int mouth) 

        

                System.out.println("I am in Cow(int ears , int mouth) constructor.....!"); 

        

These constructors can be called by creating a object like 

        new Cow(); // Cow() constructor is called 

        new Cow(4.5); // Cow(double height) constructor is called 

        new Cow(2,1); // Cow(int ears , int mouth)constructor is called 

        new Cow(4); // Cow(int leg) constructor is called 

Note : 

  • The parameterized constructor is used to initialize the instance variables of the class 
  • If you don't define a constructor, the default constructor (which has no parameters) will be generated by default. 
  • If you have any user-defined constructor, the default constructor will not be generated at all.

ALSO KEYWORD:

"constructor overloading java"

"constructor in java"

"constructor overloading and overriding in java"

"parameterized constructor in java"

"constructor overloading example"

"constructor overloading in java polymorphism"

"default constructor in java"

"copy constructor in java"