What will be the result of execution of the following code?

public class Parent {
    protected String value = "parent";

    public static void main(String[] args) {
        Parent parent = new Child();
        Child child = new Child();
        System.out.println(parent.value);
        System.out.println(child.value);
    }
}

class Child extends Parent {
    protected String value = "child";
}
Explanation
Fields of a class are not overridden as a result of inheritance. Therefore in every case the value of the field of an instance of a class will be printed out.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes