In this example, I get why the output shows (1) (2) (3) (4) At the step when Fac
ID: 3773487 • Letter: I
Question
In this example, I get why the output shows (1) (2) (3) (4)
At the step when Faculty() invokes Employee(),
Employee() invokes this.(String). and then the Employee(String s) is invoked, so that Person() is invoked.
My question is, I thought Employee() should directely invoke Person(). But the text book
xplanation shows that Employee() acutually proceed through the next line 13, this.("string") so that Employee(String s)
is invoked. Why is that? Why Empolyee constructor with String s parameter calls Person constructer and not Empolyee() which is non-arg constructor?
Explanation / Answer
Here in the main() it calls Faculty(), which means, it will first go to the parent class Employee, and from there to its parent class Person().
The constructor in the Person class is executed first. which prints the statement (1).
Then coming down to the Employee class, here in this constructor, its a collection of 2 steps, the first one being calling the same class method, with a string parameter. Note that the Employee class has two constructors, one being the default constructor, and the other one accepts one parameter of type string. But the logic here is, the default constructor, itself tries to call the second constructor of the same class, with the string (2). Therefore, the string (2) will be printed, then the second step in the default constructor of Employee class will be executed, which results in printing (3). Then, moving on to the derived class, Faculty, the default constructor is executed, which prints the statement (4).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.