Chapter 6 includes four Twist in the Tale exercises.
Purpose: This question is an example of a simple concept (private members are not accessible to a derived class) that is made to look complex by including code and options that try to divert your attention. Expect similar questions on the exam.
Answer: e
Explanation: The code fails to compile because the private members of a class can’t be accessed outside a class—not even by its derived class. The compiler can detect such attempts; this code won’t compile.
Purpose: To help you to work with a combination of
Answer: a, c
Explanation: The rules you need to follow to assign a value to an array element are the same rules you follow when you assign an object to a reference variable. Because the type of array interviewer is Interviewer, you can assign objects of classes that implement this interface. The inheritance of classes Employee, Manager, and HRExecutive and the interface Interviewer are shown in figure A.5.
As you can see in figure A.5, the classes Manager and HRExecutive implement the interface Interviewer. The class Employee doesn’t implement the interface Interviewer; hence, an object of the class Employee can’t be added to an array of type Interviewer.
From this explanation, it’s apparent that options (a) and (c) are correct and option (b) is incorrect.
Option (d) is incorrect because you can’t create objects of an interface. Option (d) tries to create an object of the interface Interviewer. Code that tries to create an instance of an interface won’t compile.
Purpose: If there is no collision with the name of a variable defined in the base class or derived class, the variable can be accessed using both super and this references from a derived class. If there is a collision, the base class variable can be accessed using the super reference.
Answer: b
Explanation: In a derived class, you’d normally use the implicit reference super to refer to a method or variable of a base class. Similarly, you’d normally use the implicit reference this to refer to a method or variable defined in the same class. A derived class contains within it an object of its base class and can access non-private members of its base class. A derived class can also refer to the members of its base class as its own members using the reference this. This approach is acceptable only if the same member isn’t defined in the derived class, that is, if there are no name collisions.
The base class Employee defines two non-private variables, name and address, which are accessible in Employee’s derived class Programmer. The class Programmer also defines an instance variable name, so the variable name should be prefixed with the explicit references super and this to refer to the variable name defined in the classes Employee and Programmer. The variable address can be referred to using both super and this in the derived class Programmer.
Option (a) is incorrect. The derived class Programmer can refer to the variable address defined in the base class using this.address. This value won’t print null.
Option (c) is incorrect. this.address won’t print blank when accessed from the derived class Programmer.
Option (d) is incorrect. The code has no compilation issues.
Purpose: Polymorphic methods should define a method’s overriding rules.
Answer: a
Explanation: Polymorphic methods exist when classes or interfaces share an inheritance relationship. A polymorphic method can be defined by a derived class if
Options (b) and (d) are incorrect. A method can’t be overridden if it defines a different parameter list.
Option (c) is incorrect. The return type of the overridden method must be the same in the base class and the derived class.
18.117.11.247