Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of method call with final class inheritance
What is the output of this Java code?
Java
final class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } public class Test { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }
Attempts:
2 left
π‘ Hint
Recall what the 'final' keyword means when applied to a class.
β Incorrect
In Java, a class declared as final cannot be extended. Here, Dog tries to extend final class Animal, causing a compilation error.
β Predict Output
intermediate2:00remaining
Output when overriding a final method
What happens when this Java code is compiled and run?
Java
class Vehicle { final void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { void start() { System.out.println("Car started"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); v.start(); } }
Attempts:
2 left
π‘ Hint
Check if final methods can be overridden.
β Incorrect
A final method cannot be overridden by subclasses. Here, Car tries to override start(), causing a compilation error.
π§ Debug
advanced2:00remaining
Identify the inheritance limitation causing error
This code tries to compile but fails. What is the cause of the error?
Java
interface Flyer { void fly(); } class Bird implements Flyer { public void fly() { System.out.println("Bird is flying"); } } class Penguin extends Bird { public void fly() { System.out.println("Penguins can't fly"); } } public class Test { public static void main(String[] args) { Flyer f = new Penguin(); f.fly(); } }
Attempts:
2 left
π‘ Hint
Consider if overriding interface methods is allowed.
β Incorrect
A subclass can override methods from its superclass even if the superclass implements an interface. Penguin overrides fly() successfully.
π Syntax
advanced2:00remaining
Which code causes a compilation error due to multiple inheritance?
Java does not support multiple inheritance with classes. Which option causes a compilation error?
Attempts:
2 left
π‘ Hint
Check how Java handles multiple inheritance with classes and interfaces.
β Incorrect
Java does not allow a class to extend more than one class. Option A tries to extend two classes, causing a compilation error.
π Application
expert3:00remaining
How many methods can a class inherit from multiple interfaces with same default method?
Given these interfaces and class, how many distinct default methods named greet() does class Friendly inherit?
Java
interface A { default void greet() { System.out.println("Hello from A"); } } interface B { default void greet() { System.out.println("Hello from B"); } } class Friendly implements A, B { public void greet() { A.super.greet(); B.super.greet(); System.out.println("Hello from Friendly"); } } public class Test { public static void main(String[] args) { Friendly f = new Friendly(); f.greet(); } }
Attempts:
2 left
π‘ Hint
Think about how Java handles default methods with same signature in multiple interfaces.
β Incorrect
Class Friendly inherits two default greet() methods, one from each interface A and B. It must override greet() to resolve the conflict and can call both using InterfaceName.super.greet().