Protected access through different reference types

Three Java classes are each in their own source file. Animal and Keeper are in package zoo.core; Lion is a subclass of Animal in package zoo.exhibits. Judge each labelled statement on its own, as if every other labelled statement were absent.


Java
package zoo.core;

public class Animal {
    protected int age;
    protected void feed() { }
    int tag;                  // no modifier
    private double weight;
    public String name;
}


Java
package zoo.core;

public class Keeper {
    void visit(Animal a) {
        a.age = 2;            // K1
        a.feed();             // K2
        a.tag = 9;            // K3
        a.weight = 50.0;      // K4
    }
}


Java
package zoo.exhibits;

import zoo.core.Animal;

public class Lion extends Animal {
    void check(Lion other, Animal generic) {
        age = 3;              // L1
        this.feed();          // L2
        other.age = 4;        // L3
        generic.age = 5;      // L4
        generic.feed();       // L5
        tag = 7;              // L6
        generic.name = "Leo"; // L7
    }
}


Give each answer as a whole number: the number of Keeper statements (K1 to K4) that compile, the number of Keeper statements that fail, the number of Lion statements (L1 to L7) that compile, and the number of Lion statements that fail.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Access Modifiers Questions: