MainMenu

Home Java Overview Maven Tutorials

Monday, 19 April 2021

Constructor Chaining In java

What is Constructor chaining in JAVA



Definition :-

The process of invoking a sequence of constructors upon initialization of a class object is called constructor chaining.
Constructor chaining is useful when you want to invoke multiple constructors, one after another, by initializing only one instance.

Constructor chaining can be done in two ways:
Within same class: It can be done using this() keyword for constructors in same class
From base class: by using super() keyword to call constructor from the base class.



package ChandanPack1;

class A
{
A()
{
this(0);
System.out.println("I am inside A class constructor");
}
A(int x)
{
this(2, 4);
System.out.println("Superclass second constructor" + " " +x);
}
A(int l, int m)
{
System.out.println("multiply is " + " " + (l*m));
}
}

public class BajiPractice extends A
{
BajiPractice()
{
this(10);
System.out.println("i am inside first constructor");
}
BajiPractice(int x)
{
this(2, 80);
System.out.println("number is:->" + " "+ x);
}
BajiPractice(int a, int b)
{
super();
System.out.println("the sum is" + " " +(a+b));
}
public static void main(String args[])
{
System.out.println("constructor chaining started");
BajiPractice obj = new BajiPractice();
}
}



Output:- constructor chaining started
multiply is 8
Superclass second constructor 0
I am inside A class constructor
the sum is 82
number is:-> 10
i am inside first constructor

12 comments:

  1. Nice Information...thanks for sharing this good information & i have some information about the Real Estate luxurious projects in Hyderabad, Telangana, India...
    This Project will helpful for those who are looking to buy their Dream Home with low budget..
    Thanks for Sharing..!.!

    Ambience Courtyard
    The Botanika
    Sumadhura Horizon
    Spring Is In The Air
    Aditya Athena

    ReplyDelete
  2. Nice Blog..Thanks for sharing this helpful information & i found some information about Sumadhura Horizon and it is a residential project developed by Sumadhura Infracon Pvt. Ltd. located in Kondapur, Hyderabad. It is provides 3BHK luxury residential flats.

    Sumadhura Horizon

    ReplyDelete
  3. Nice Blog..Thanks for sharing this helpful information & i found some information about Sumadhura Horizon and it is a residential project developed by Sumadhura Infracon Pvt. Ltd. located in Kondapur, Hyderabad. It is provides 3BHK luxury residential flats.

    Sumadhura Horizon Hyderabad
    Sumadhura Horizon Kondapur

    ReplyDelete
  4. I really like your blog....thanks for sharing this good information...& I found some information about The Ambience Courtyard and it is Developed by Ambience Group, located in Manikonda, Hyderabad. It provides 2, 2.5 & 3BHK flats available in affordable prices.

    Ambience Courtyard
    Ambience Courtyard Manikonda Hyderabad

    ReplyDelete
  5. I really like your blog....thanks for sharing this good information...& I found some information about The Ambience Courtyard and it is Developed by Ambience Group, located in Manikonda, Hyderabad. It provides 2, 2.5 & 3BHK flats available in affordable prices.

    Ambience Courtyard Hyderabad
    Ambience Courtyard Manikonda

    ReplyDelete
  6. Nice Blog...Thanks for sharing with us...and i have some information about The Botanika and it is a new residential project located in Gachibowli, Hyderabad. This project offers 3 BHK Luxury Apartments in Affordable Prices. Contact Us@ 98492 05577

    The Botanika

    ReplyDelete
  7. Nice Blog...Thanks for sharing with us...and i have some information about The Botanika and it is a new residential project located in Gachibowli, Hyderabad. This project offers 3 BHK Luxury Apartments in Affordable Prices. Contact Us@ 98492 05577

    The Botanika Hyderabad
    The Botanika Gachibowli

    ReplyDelete
  8. Nice Article..Thanks for sharing this helpful information please do share more ideas & i found some information about:

    Aditya Athena
    Sri Aditya Athena

    ReplyDelete
  9. Nice Article..Thanks for sharing this helpful information please do share more ideas & i found some information about:

    Aditya Athena Shaikpet
    Aditya Athena Hyderabad

    ReplyDelete
  10. Great Blog...thanks for sharing with us...& I have some information about Spring Is In The Air Urbanrise & it is a residential apartments developed by Manhattan Condos, it is located in Miyapur, Hyderabad. It provides 2,3bhk flats available in affordable prices.

    Spring Is In The Air
    Spring Is In The Air Urbanrise

    ReplyDelete
  11. Great Blog...thanks for sharing with us...& I have some information about Spring Is In The Air Urbanrise & it is a residential apartments developed by Manhattan Condos, it is located in Miyapur, Hyderabad. It provides 2,3bhk flats available in affordable prices.

    Spring Is In The Air Urbanrise Miyapur
    Spring Is In The Air Urbanrise Hyderabad

    ReplyDelete