123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Environment >> View Article

What Is The Need Of Constructor Chaining In Java

Profile Picture
By Author: Infocampus
Total Articles: 33
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

What is Constructor Chaining: Calling a constructor from another constructor , is known as constructor chaining.If we want to access the properties of any constructor then we have to use constructor chaining.

We know that constructor is neither static nor non static. So we can’t inherit the constructor.But using constructor chaining we can acquire the properties of another constructor.

Note: Constructor is a special type of method that always holds the class name. It is not a method but it consist of the same behaviour as a method.

Properties of Constructor :

[1]It is same as the class name.
[2]It does not having any return type or return statement.
[3]It has access modifiers.
[4]It is neither static nor nonstatic.

Java/j2ee classes bangalore
How can we achieve the constructor chaining ?
There is two ways to achieve the constructor chaining:

[1]super() : super() (call to super) is used to call the super class constructor. That mean sub class is able to access the properties of constructor which are present in super class. For accessing the super class ...
... constructor properties inheritance is must.

If user is not defined the super() then compiler generates as the first statement in the class.But if user defined constructor is there compiler never generate the super().

[2]this(): this()(call to this) is used to call the same class constructor. That mean we can achieve the properties of constructor which are present in the same class. For using this() constructor overloading is necessary. Without constructor overloading we can’t get the properties of same class.

Java course in bangalore
Note: super() and this() should be the first statement. Because of that both can’t be present in the same class.

What is Constructor Overloading ?
In a single class multiple constructor exist with the same name but differs in number of parameters , type of parameters and position of parameters.

What is need of constructor chaining ?

[1]Access the properties: Constructor chaining is used to access the properties of constructor in the same class or in another class.

[2]Code optimization :Using constructor chaining we achieve code optimization ,means lines of code get reduced.
Java training center bangalore
[3]Maintenance easy : Using constructor chaining maintenance easy because if we keep the common functionality in super class constructor.So we need to change only in super class constructor.

[4]Memory will get reduced : If same code will repeat at multiple times then more memory will used. to avoid this we use constructor chaining, we access the same code.Then less memory used.

Difference between constructor and method :

[1] Constructor always holds the class name, but method name should be different ,method name should not be same as the class name.

[2]Constructor is neither static nor non static but method is always static or non static.

best core java training in bangalore
[3]Constructor is not having return type or return statement. Method is always having return type. Method always returns a value. If we don’t want any value to be return then we use void return type.

Difference between super() and this()

1) super() is used to call super class constructor, this() is used to call same class constructor.

2) For super() inheritance is necessary because for achieving the super class constructor we use the concept of sub and super class.
For this() constructor overloading is necessary.

3)If userdefined super() is not there compiler generate super().
But this() is not generate by the compiler, we have to explicitly define the this().

Advance java training in bangalore
This is the short description about the constructor chaining to get the good knowledge on java for development purpose and to work on the live project join Infocampus Software Training institute.

Author : Infocampus provides java/j2ee classes bangalore with 100% placement assistance.
Complete practical classes are available on java course. 4 days free demo classes provided for java training. Training is given by the 10+ years experienced trainers.
Learn oops concept ,thread ,packages,spring ,hibernate etc in best core java training institute.
Mock test and mock interviews are conducted every weekend on Core and Advance java.
After every topic assignment is provided for the preparation on advance java training.
To get the free demo classes call at 9738001024 or visit at :
http://www.infocampus.co.in/java-training-bangalore.html

Total Views: 536Word Count: 671See All articles From Author

Add Comment

Environment Articles

1. Revolutionizing Software Architecture: The Rise Of Cloud-native Development
Author: avadhesh banerjee

2. Green Growth Solutions: Exploring The Benefits Of Plant Growth Promoters
Author: aaditri biswas

3. Sustainable Fish Farming: Harnessing The Benefits Of Biofloc Technology
Author: vanessa dolan

4. Emerging Technologies In Industrial Solar Installation
Author: JJ Solar

5. Keeping Your Marietta Home Safe: Carpenter Bee Control And Pest Management With Urban Wildlife Control
Author: Richard

6. Are Eco-friendly Egg Cartons The Key To A Greener Future?
Author: poultry cartons

7. Waste Classification Report Sydney – How It Is Important Today
Author: Sydney Environmental Group

8. How Marine Life Is Being Disrupted By Plastic Pollution?
Author: James Williams

9. The Top 5 States Leading The Solar Revolution
Author: Anand Mishra

10. Investing In Solar Panel
Author: JJ SOLAR

11. Buying Solar Appointments Vs. Generating Leads In-house: A Weighing Game
Author: Shan Tait

12. Emmanuel Katto's Explains The Plight Of African Coffee Farmers Facing Climate Change
Author: arnoldmukasa

13. Teach You To Choose A Good Gas Leak Detector
Author: JINGXUN

14. Maximizing Yield Potential: A Guide To Effective Biostimulant Application
Author: aaditri biswas

15. Nature's Touch: Exploring The Effectiveness Of Organic Surface Cleaning Solutions
Author: aaditri biswas

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: