Skip to content
CodeKruPro

A parent reference to a child object

Predict the outputEasyJava onlyAsked in: Cognizant, Wissen Technology
Main.java
class Animal {
    String sound() {
        return "...";
    }
}

class Dog extends Animal {
    @Override
    String sound() {
        return "Woof";
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        Animal b = new Animal();
        System.out.println(a.sound());
        System.out.println(b.sound());
    }
}

Graded by exact match. Spaces at the ends of lines and blank lines at the end do not count.

to answer and keep your progress. Reading is free for everyone.