answersLogoWhite

0


Best Answer

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

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

The current class is usually the first to be called and then its parent and so on. The object class will be the last constructor to be called

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In what order are the class constructors called when a derived class object is created?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When constructor function are called in java?

Constructors are called during object creation.


What is concession constructor?

Constructors are basically used to evoke methods of a class creating its object..and as far as i know there is no constructor called concession constructor..


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


What is the purpose of a C plus plus constructor?

The constructor in C++ is a function that runs when an object is created. It is used to initialize the object. Types of constructors include default constructors (no arguments), copy constructor (one argument of same type as object), conversion constructors (one argument of some other type), and other constructors (all other cases). If you do not provide a default constructor, the object will not be initialized. If you do not provide a copy constructor, the compiler will blindly copy the attributes of the old object into the new object whenever a copy is made, such as in a function call with the object as an argument. This may or may not be safe, especially if any of the attributes are pointers, because that creates the situation of two pointers to the same region of memory. In that case, if that region of memory is an object, then when the object is destroyed, so will the pointed to object, and that will leave the original copied object in an invalid state, with its pointers referencing deleted memory.

Related questions

When constructor function are called in java?

Constructors are called during object creation.


Can you have virtual constructor?

The short answer is no, you cannot. The long answer is that constructors are not functions that can be called directly, and overriding a base class constructor would have no practical meaning since the derived class is itself responsible for calling its own base class constructors (whether implied by omission or explicitly via the derived class' initialisation list). Even so, the derived class isn't calling the base class constructor directly (that's why constructors have no return value; the actual call is made behind the scenes). The base class itself may be derived in which case its base class must be constructed before it can be constructed. This is the complete reverse of how a virtual function behaves, and is the reason that destructors can be virtual but constructors cannot. When a base class is destroyed, all its derivatives must be destroyed first, starting with the most-derived class of object. This can only be achieved through virtual destruction.


What are parameterized constructors with example in RDBMS?

Parameterized constructors (or more simply "constructors") allow you to create a new instance of a class while simultaneously passing arguments to the new instance. Constructors are essential for object oriented programming since they allow user-defined construction code to be passed parameters by the creator of the instance. They simplify client code by allowing a new object instance to be created and initialized in a single expression.


What is the use of constructors?

Constructors are used in object-oriented programming languages to create usable instances of abstract data types (classes).


What is concession constructor?

Constructors are basically used to evoke methods of a class creating its object..and as far as i know there is no constructor called concession constructor..


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


What is the purpose of a C plus plus constructor?

The constructor in C++ is a function that runs when an object is created. It is used to initialize the object. Types of constructors include default constructors (no arguments), copy constructor (one argument of same type as object), conversion constructors (one argument of some other type), and other constructors (all other cases). If you do not provide a default constructor, the object will not be initialized. If you do not provide a copy constructor, the compiler will blindly copy the attributes of the old object into the new object whenever a copy is made, such as in a function call with the object as an argument. This may or may not be safe, especially if any of the attributes are pointers, because that creates the situation of two pointers to the same region of memory. In that case, if that region of memory is an object, then when the object is destroyed, so will the pointed to object, and that will leave the original copied object in an invalid state, with its pointers referencing deleted memory.


Are constructors allowed to have a return statement in them?

NO...The reason being, when you return something, someone is supposed to receive what you are returning...So when the object is not yet created, there wont be any body to receive it.


What is a constructor with value zero?

Constructors have no value, zero or otherwise. That is, constructors cannot return a value. This is because constructors are not functions in the sense you cannot call a constructor directly. Constructors are invoked in the background when you instantiate an object of the class, thus any return value would be lost in the background, and would therefore not be visible to the invokee.


How can you identify a constructor from among any other methods in a class?

Constructors have the same identifier as that of the class, so if the name of your class is Book then your constructor must also be named Book. Constructors have no return type, not even void.


What is copy constructor in object oriented programming?

The purpose of constructor in object oriented programming is to initialize data. Likewise copy constructors are used to initialize an object with data from another object.