73.7k views
5 votes
Assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where_______ indicates the required parameters for the constructors:

Person p = new Person(...);
int m1 = p.getMoney(); // assignment 1
p = new Student(...);
int m2 = p.getMoney(); // assignment 2
if (m2 < 100000)
p = new Employee(...);
else if (m1 > 50000)
p = new Retired(...);
int m3 = p.getMoney(); // assignment 3

Refer to above code The reference to getMoney() in assignment 1 is to the class:________
a. Person
b. Student
c. Employee
d. Retired
e. This cannot be determined by examining the code

1 Answer

2 votes

Answer:

a. Person

Step-by-step explanation:

The reference getMoney() is a method that is part of the Person class. This can be proven with the code since subclasses can access methods from the parent class but a parent class cannot access methods from the child classes. Since Student, Employee, and Retired are all child classes that extend to the Person parent class then they can access getMoney(), but unless getMoney() is in the Person class, then a Person object shouldn't be able to access it. Since the first object created is a Person object and it is accessing the getMoney() method, then we can assume that the getMoney() method is within the Person class.

User Masum
by
7.1k points