Important Java Mcq 1

How many changes need to be made to the classes below to properly override the watch() method?

package entertainment;
       class Television {
                 protected final void watch() {}
       }
       public class LCD extends Television {
                  Object watch() {}
        }

a. One

b. Two

c. Three

d. None; the code compiles as it is.

Answer:

c. Three

Explanation

First, the watch() method is marked final in the television class. The method
watch() should be removed as final in order to make the method inside the LCD class get
compiled. Second, the return type is void and objects are not covariant. One of them would
Have to be changed for the override to be compatible. Finally, the access modifier in the
child class must be the same as that in the parent class.

Related MCQs
Aniket Malik

Aniket Malik

CERTIFIED TUTOR/TRAINER WITH 300+ REVIEWS

Facing difficulty with

this concept?