Q: What is the output of the below Java program with method overriding? class Cat { int jumpingHeight(int weight) { System.out.println(10); return 10; } } class WildCat extends Cat { void jumpingHeight(int weight) { System.out.println("20"); } } public class MethodOverriding3 { public static void main(String[] args) { WildCat wc = new WildCat(); wc.jumpingHeight(30); } }
Solution: If the argument list is the same, the return types can not be the incomptible-types. So, the compiler reports an error "The return type is incompatible with Cat.jumpingHeight(int)".
Q: What is the output of the below Java program with Method Overriding? class Sparrow{ } class BigSparrow extends Sparrow { } class Cat2 { Sparrow jumpingHeight(int weight) { System.out.println(40); return new Sparrow(); } } class WildCat2 extends Cat2 { BigSparrow jumpingHeight(int weight) { System.out.println("50"); return new BigSparrow(); } } public class MethodOverriding4 { public static void main(String[] args) { WildCat2 wc = new WildCat2(); wc.jumpingHeight(80); } }
Solution: It is perfectly alright to use a subclass type return type when overriding a method in Java. BigSparrow is the subclass of Sparrow. Always, the overriding method will be called.
Q: A method of a Superclass can not override the method of the Subclass. State TRUE or FALSE.
Solution: True. Only subclass methods can override the methods of a superclass.
Q: Method overriding increases the burden on the JVM in terms of runtime checks and resolution. State TRUE or FALSE.
Solution: Yes. The to be called at runtime is decided at runtime based on successful or failed Overriding.
Q: What are the advantages of Method Overriding in Java?
Solution: no solution
Q: An Overridden method is the method of ____ class and the overriding method is the method of ___ class.
Solution: An Overriding method belongs to the Subclass and the Overridden method belongs to the Superclass.
You Have Score    | /6 |