Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed 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.
image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Contemporary Issues In Database Design And Information Systems Development

Authors: Keng Siau

1st Edition

1599042894, 978-1599042893

More Books

Students also viewed these Databases questions