Question

When the object of a derived class is instantiated, discuss the chain of constructor calls that...

When the object of a derived class is instantiated, discuss the chain of constructor calls that begin in the program. Discuss which constructor will be executed at the end. Can constructors be inherited?

Homework Answers

Answer #1

When an object is created for a class, the default constructor of that class is automatically invoked to initialize the members of the class.

When object of derived class is instanciated, it is clear that the default constructor of the derived class will be invoked but before that the default constructor of all of the base classes will be invoke, i.e the order of invokation is that the base class’s default constructor will be invoked first and then the derived class’s default constructor will be invoked.

The data members and member functions of base class come automatically in derived class based on the access specifier. But the definition of these members exists in base class only. So when we create an object of derived class, all the members of derived class must be initialized whereas the inherited members in derived class can only be initialized by the base class’s constructor as the definition of these members exists in base class only. This is why the constructor of the base class is called first to initialize all the inherited members.

Only members are inherited, and a constructor is not considered a member.

To understand why constructors are not inherited, consider that inheritance in OOP is part of the mechanism that allows an object to be treated as another, more general object. This requires that all data members and all methods are inherited.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1.Assume we've instantiated a static object named xyz of some class. When will xyz go out...
1.Assume we've instantiated a static object named xyz of some class. When will xyz go out of scope? 2.Assume we've instantiated a dynamic object of some class. We used a pointer variable named p. Code a statement to free the memory occupied by abc.
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
The UML diagram bellow shows an example for simple chain of inheritance relationship where ClassC inherits...
The UML diagram bellow shows an example for simple chain of inheritance relationship where ClassC inherits ClassB’s members, including the ones that ClassB inherited from ClassA. ClassA constructor has a println statement that prints "Hello This is ClassA", ClassB constructor has a println statement that prints "Hello this is ClassB" and ClassC constructor has a println statement that prints "Hello this is ClassC". ClassC is the driver class. If you create an object of ClassC,you will get the following output:...
no need to explain. True or False 1. A constructor is a special Class member method....
no need to explain. True or False 1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a VacationPackage with provided values. Method Summary All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object other) Provides a logical equality comparison for VacationPackages and any other object type. double getAmountDue() This method provides the remaining amount due to the travel agent for this trip less any deposit made upfront. abstract double getDepositAmount() This method provides the required...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT