Ask Question
26 December, 14:45

The getValue () method is overridden in two ways. Which one is correct?

1.

public class Test {

public static void main (String[] args) {

A a = new A ();

System. out. println (a. getValue ());

}

class B {

public String getValue () {

return "Any object";

}

class A extends B {

public Object getValue () {

return "A string";

}

2.

public class Test {

public static void main (String[] args) {

A a = new A ();

System. out. println (a. getValue ());

}

class B {

public Object getValue () {

return "Any object";

}

class A extends B {

public String getValue () {

return "A string";

}

a. I

b. II

c. Both I and II

d. Neither

+4
Answers (1)
  1. 26 December, 15:05
    0
    Hi there KatiesTomato! The answer is: b. II

    Explanation:

    Java allows us to override a method defined in the main or Parent class by re-defining it in the child class. In the question, the first option gives us code that will throw an error because the String method getValue () in class B is being overridden by a method in class A which returns an Object which is also a String. So the compiler will throw an error that type Object is not compatible with String. Option 2 will compile successfully and return a String because the Object is allowed to be converted to a String.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “The getValue () method is overridden in two ways. Which one is correct? 1. public class Test { public static void main (String[] args) { A ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers