Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

What is the use of constructor chaining in Java?

In the below example where can I access this new object “James Bond” for future use?

import java.util.*;

 class Employee 

{

 private String name;

 private double salary;
public Employee() 

this("James Bond", 34000);

 }

 public Employee(String n, double s)

 { 

name = n;

 salary = s;

 }

 public String getName()

 {

 return name;

 }

 public double getSalary()

 {

 return salary;

 }

 public static void main(String[] args)

 {

 Employee a = new Employee();

 }

 }

1 Answer

0 votes
by (13.1k points)

Constructor chaining is the process of calling one constructor from another with respect to the current object.

It ensures the initialization of the sub-class’ object starts with the initialization of data members of the superclass.

In the above example the object “James Bond” is called when the second constructor is being initialized in the main class.

Want a Java tutorial? Check out the Java training from Intellipaat.

Related questions

0 votes
1 answer
asked Apr 3, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 20, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)

Browse Categories

...