A.6 Chapter 6: Working with inheritance

Chapter 6 includes four Twist in the Tale exercises.

A.6.1 Twist in the Tale 6.1

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.

A.6.2 Twist in the Tale 6.2

Purpose: To help you to work with a combination of

  • Arrays
  • Assigning an object of a derived class to a reference variable of the base class
  • Assigning an object of a class that implements an interface to a reference variable of the interface

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.

Figure A.5. UML notation of inheritance hierarchy of the classes Employee, Manager, and HRExecutive and the interface Interviewer

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.

A.6.3 Twist in the Tale 6.3

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.

A.6.4 Twist in the Tale 6.4

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

  • The derived class implements an abstract method defined in a base class or interface
  • The derived class overrides a non-abstract method defined in a base class

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247