Ask Question
14 September, 02:24

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

+3
Answers (1)
  1. 14 September, 03:40
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers