Question: Q17 answer is D. Q40 ans is A. Should subclass object of type superclass call superclass or subclass method. In 17, j calls subclass method

 Q17 answer is D. Q40 ans is A. Should subclass object Q17 answer is D. Q40 ans is A. Should subclass object of type superclass call superclass or subclass method. In 17, j calls subclass method while in 40 obj1 calls superclass method. Both j and obj1 have the condition: object instantiated in subclass of type superclass. There is a discrepancies between these two questions. Please clearly explain the correct reasoning and my mistake.
of type superclass call superclass or subclass method. In 17, j calls

17. Consider the following class definitions public class Hero private String name; private int power: public Hero(String n, int p) 1 name = n; power = p: } public void powerUp(int p) power + p: } public int showPower() return power; } public class Superhero extends Hero public SuperHero(String n, int p) 1 super(n. p): 1 public void powerUp(int p) ! super powerUpp * 2): The following code segment appears in a class other than Hero and Superhero. Hero j = new SuperHero("JavaHero". 50): j.powerUp(10): System.out.println(j.showPower): What is printed as a result of executing the code segment? (A) 10 (B) 20 (C) 60 (D) 70 (E) 100 40. Consider the following class definitions. public class A public String message(int i) { return "A" + i; } 1 public class B extends A public String message(int i) { return "B" + i; } } The following code segment appears in a class other than A or B. A obj1 = new BO; // Line 1 B obj2 = new BO: 1/ Line 2 System.out.println(obj1.message(3)); // Line 3 System.out.println(obj2. message(2)); // Line 4 Which of the following best explains the difference, if any, in the behavior of the code segment that will result from removing the message method from class A ? (A) The statement in line 3 will cause a compiler error because the message method for objl cannot be found. (B) The statement in line 4 will cause a compiler error because the message method for obj2 cannot be found. (C) As a result of the method call in line 3, the message method in class B will be executed instead of the message method in class A. (D) As a result of the method call in line 4. the message method in class B will be executed instead of the message method in class A. (E) The behavior of the code segment will remain unchanged

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!