Ordering case dominance

Report a typo

You are given a class hierarchy where some classes extend others as shown below.

class Animal {}
class Mammal extends Animal {}
class Canine extends Mammal {}
class Dog extends Canine {}

In the switch statement below, the case labels are out of order and cause a dominance error because more general types appear before more specific ones. Reorder the code snippet so that the switch compiles and runs properly.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                case Animal a   -> System.out.println("An animal");
              
                }
              
                switch (animal) {
              
                public static void describeAnimal(Animal animal) {
              
                public class DominanceFixer {
              
                case Mammal m   -> System.out.println("A mammal");
              
                }
              
                case Canine c   -> System.out.println("A canine");
              
                case Dog d      -> System.out.println("A dog");
              
                }
              

Create a free account to access the full topic