wellnwill Marathahalli Bangalore

Constructor overloading


Creating multiple constructors with different parameters (arguments) is known as constructor overloading. In constructor overloading, parameter ((arguments) must be differ in terms of A. Number of arguments B. Type of arguments C. Order of arguments

class Bulb
{  
 int watt;
 float price;
 String color;
 boolean state;
 
 Bulb()
 {
  watt=10;
  price=50f;
  color="white";
  state=true;
 }
 
  Bulb(int w, float p, String clr, boolean b)
  {
   watt=w;
   price=p;
   color=clr;
   state=b;
  }
  
  Bulb(int w, String clr, float p, boolean b)
  {
   watt=w;
   price=p;
   color=clr;
   state=b;
  }

void displayDetails()
 {
 System.out.println("watt: "+watt);
 System.out.println("price: "+price);
 System.out.println("color: "+color);
 System.out.println("state: "+state);
 }
} 
    
class Test157{  
 public static void main(String args[]){ 
 
  Bulb b0=new Bulb(); 
  Bulb b1=new Bulb(100,15.50F ,"white",true); 
  Bulb b2=new Bulb(200,"blue",25.75F,false); 
  System.out.println("-----Details of Bulb b0-----");
  b0.displayDetails();
  System.out.println("-----Details of Bulb b1-----");
  b1.displayDetails();
  System.out.println("-----Details of Bulb b2-----");
  b2.displayDetails();
 }  
}  









• We can use access modifiers i.e. public, private, protected, default with constructor. • We cannot use non-access modifiers such as static, abstract, synchronized, volatile with constructor.


Valid constructor with access modifires


Invalid constructor with non-access modifier


• Every class in java including abstract class can contain constructor but interface cannot contain constructor.


abstract class Bulb
{  
    Bulb()
	{
	System.out.println("constructor is invoked");
	} 
	
	public static void main(String args[])
	{  
              System.out.println("inside main()");
    }
} 
Output-inside main()
	
abstract class Bulb
{  
    Bulb()
	{
    super();
	System.out.println("constructor is invoked");
	} 
	
	public static void main(String args[])
	{  
              System.out.println("inside main()");
    }
} 
Output-inside main()

Inside every constructor call to this i.e. this() or call to super i.e. super() must be first statement if we are not writing this() or super() then compiler will always add super().



We can use super() or this() inside constructor only. The statement super() or this() must be first statement inside constructor but not both simultaneously.


class Test160
{  
    Test160()
	{
	 super();
	 this();
	System.out.println("constructor is invoked");
	} 
	
	public static void main(String args[])
	{  
            System.out.println("inside main()");
    }
} 
Output-CTE-call to this must be first statement in constructor.	
class Test161
{  
    void Test161()
     {
	   super();
	   System.out.println("Test161() is invoked");
	 } 
     
	 public static void main(String args[])
	 {  
              System.out.println("inside main()");
     }
} 
Output-call to super must be first statement in constructor.

class Test161
{  
    void Test161()
       {
	   super();
	   System.out.println("Test161() is invoked");
	   } 
        
   public static void main(String args[])
    {  
              System.out.println("inside main()");
    }
} 
Output-call to super must be first statement in constructor.	


Inheritance and overriding concepts are not applicable for the constructor. Calling a method recursively is possible but calling a constructor recursively will give compile time error.


class Test162
{  
    void test()
    {
	 test1();
	 System.out.println("constructor is invoked");
	} 
	void test1()
    {
	 test();
	 System.out.println("constructor is invoked");
	} 
	public static void main(String args[])
	{	
             System.out.println("inside main()");
    }
} 
Output-inside main()



	

class Test163
{  
     void test()
     {
       test1();
       System.out.println("inside test()");
     } 
     
	 void test1()
     {
       test();
       System.out.println("inside test1()");
     } 
        
	public static void main(String args[])
	{	
        Test163 t1=new Test163();
        t1.test();
        System.out.println("inside main()");
    }
} 
Output-CTS, there will be recursive method calling.


If Super class contains any arguments constructor then while defining child classes we have keep attention with respect to constructors because there should be a constructor relationship to achieve functionally of object class (Parent class of all the java classes).



Invalid code:


Suppose super class constructor is throwing any checked exception then child class constructor must have to throw the same checked exception or its parent exception.


import java.io.*;

class Bulb{
          
       Bulb() throws IOException
       {
        
       }
}
class Surya extends Bulb
{

      Surya()
       {
        
       }
}
class Test166
{
      public static void main(String args[])
      {	
      System.out.println("inside main()");
      }
}

Output-CTE-unreported exception IOException:must be caught or declared to be thrown.
import java.io.*;

class Bulb{
          
       Bulb() throws IOException
       {
        
       }
}
class Surya extends Bulb{

      Surya()throws IOException
       {
        
       }
}
class Test166
{
      public static void main(String args[])
	  {	
      System.out.println("inside main()");
      }
}
Output-inside main()


Footer with Map

Top