answersLogoWhite

0

Why you use constructor chaining?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

Constructor Chaining

We know that constructors are invoked at runtime when you say new on some class type as follows:

Lamborghini h = new Lamborghini();

But what really happens when you say new Lamborghini() ? (Assume Lamborghini extends Car and Car extends Object.)

1. Lamborghini constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(),

2. Car constructor is invoked (Car is the superclass of Lamborghini).

3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Car extends Object even though you don't actually type "extends Object" into the Car class declaration. It's implicit.) At this point we're on the top of the hierarchy.

4. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable.

5. Object constructor completes.

6. Car instance variables are given their explicit values (if any).

7. Car constructor completes.

8. Lamborghini instance variables are given their explicit values (if any).

9. Lamborghini constructor completes.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use constructor chaining?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.

Related questions

What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


Why you use constructor overloading?

When we are initializing our object with different internal state then we can use the constructor overloading.


Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


Can somebody give me as much information as possible about chaining Pokemon in Pokemon Diamond and Pearl?

Chaining Pokemon means seeing a Pokemon over and over with the pokeradar. you will know if your chaining if you use the pokeradar appliance in the poketch. if more questions about chaining go to youtube and type in your question!! pokemaster5


Use of constructor?

to create an instance of object


Why constructor use in java?

To create objects of classes


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


Why you use constructors?

To create an instance of the class that implementing that constructor


Why Java always provides a default constructor to class when you use another constructor default constructor remove?

Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.