Challenge - 5 Problems
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of overridden method with super call
What is the output of this Java program?
Java
class Parent { void show() { System.out.println("Parent show"); } } class Child extends Parent { @Override void show() { System.out.println("Child show"); super.show(); } } public class Test { public static void main(String[] args) { Parent obj = new Child(); obj.show(); } }
Attempts:
2 left
π‘ Hint
Remember that the overridden method in the child class calls super.show() after printing.
β Incorrect
The object is of type Child, so Child's show() runs. It prints "Child show" then calls super.show(), which prints "Parent show".
β Predict Output
intermediate2:00remaining
Return type covariance in method overriding
What will be the output when this Java code runs?
Java
class Animal {} class Dog extends Animal {} class Parent { Animal getAnimal() { System.out.println("Parent getAnimal"); return new Animal(); } } class Child extends Parent { @Override Dog getAnimal() { System.out.println("Child getAnimal"); return new Dog(); } } public class Test { public static void main(String[] args) { Parent p = new Child(); Animal a = p.getAnimal(); } }
Attempts:
2 left
π‘ Hint
Check if the return type in Child is allowed to be a subclass of Parent's return type.
β Incorrect
Java allows covariant return types in overriding. Child's getAnimal returns Dog, a subclass of Animal, so it compiles and runs. The overridden method in Child is called, printing "Child getAnimal".
β Predict Output
advanced2:00remaining
Access modifier rules in method overriding
What error or output occurs when compiling and running this code?
Java
class Parent { protected void display() { System.out.println("Parent display"); } } class Child extends Parent { @Override void display() { System.out.println("Child display"); } } public class Test { public static void main(String[] args) { Parent p = new Child(); p.display(); } }
Attempts:
2 left
π‘ Hint
Check if the access modifier in Child's display is compatible with Parent's.
β Incorrect
Parent's display is protected. Child's display has default (package-private) access, which is weaker. Java does not allow reducing visibility in overriding, so this causes a compilation error.
β Predict Output
advanced2:00remaining
Exception rules in overridden methods
What happens when compiling this code?
Java
class Parent { void process() throws Exception { System.out.println("Parent process"); } } class Child extends Parent { @Override void process() throws RuntimeException { System.out.println("Child process"); } } public class Test { public static void main(String[] args) { Parent p = new Child(); try { p.process(); } catch (Exception e) { System.out.println("Caught Exception"); } } }
Attempts:
2 left
π‘ Hint
Check if RuntimeException is allowed as an overridden method exception.
β Incorrect
Child's process throws RuntimeException, which is unchecked and allowed. The method runs and prints "Child process". No exception is thrown, so catch block is not executed.
π§ Conceptual
expert2:00remaining
Which statement about method overriding is true?
Select the only true statement about method overriding in Java.
Attempts:
2 left
π‘ Hint
Think about access modifiers, exceptions, return types, and static methods rules.
β Incorrect
In Java, overriding methods must have the same or covariant return type. Access modifiers cannot be more restrictive. Checked exceptions must be the same or narrower. Static methods are hidden, not overridden.