0
0
Javaprogramming~20 mins

Inheritance limitations in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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();
    }
}
ARuntime error
BAnimal sound
CDog barks
DCompilation error due to Dog extending final class Animal
Attempts:
2 left
πŸ’‘ Hint
Recall what the 'final' keyword means when applied to a class.
❓ Predict Output
intermediate
2: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();
    }
}
ACar started
BCompilation error due to overriding final method
CRuntime error
DVehicle started
Attempts:
2 left
πŸ’‘ Hint
Check if final methods can be overridden.
πŸ”§ Debug
advanced
2: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();
    }
}
ACompilation error because Penguin cannot override fly() from Bird
BPenguin cannot override fly() because Bird already implements Flyer
CNo error; output is 'Penguins can't fly'
DRuntime error due to Penguin fly() method
Attempts:
2 left
πŸ’‘ Hint
Consider if overriding interface methods is allowed.
πŸ“ Syntax
advanced
2: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?
A
class A {}
class B {}
class C extends A, B {}
B
interface A {}
interface B {}
class C implements A, B {}
C
interface A {}
class B {}
class C extends B implements A {}
D
interface A {}
interface B {}
interface C extends A, B {}
Attempts:
2 left
πŸ’‘ Hint
Check how Java handles multiple inheritance with classes and interfaces.
πŸš€ Application
expert
3: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();
    }
}
ATwo default greet() methods inherited, one from A and one from B
BOne default greet() method inherited from interface B
COne default greet() method inherited from interface A
DNo default greet() methods inherited because Friendly overrides greet()
Attempts:
2 left
πŸ’‘ Hint
Think about how Java handles default methods with same signature in multiple interfaces.