0
0
Javaprogramming~15 mins

Protected access modifier in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Protected Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of accessing protected member in subclass

What is the output of this Java code?

Java
package pack1;

public class Parent {
    protected String message = "Hello from Parent";
}

package pack1;

public class Child extends Parent {
    public void printMessage() {
        System.out.println(message);
    }
}

package pack1;

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.printMessage();
    }
}
AHello from Parent
BNo output
CRuntime error: NullPointerException
DCompilation error: message has protected access in Parent
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Accessing protected member from different package subclass

What will be the output of this code when run?

Java
package pack1;

public class Parent {
    protected String message = "Hello from Parent";
}

package pack2;

import pack1.Parent;

public class Child extends Parent {
    public void printMessage() {
        System.out.println(message);
    }
}

package pack2;

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.printMessage();
    }
}
ACompilation error: message has protected access in Parent
BRuntime error: NullPointerException
CHello from Parent
DNo output
Attempts:
2 left
💻 code output
advanced
2:00remaining
Accessing protected member from non-subclass in different package

What will happen when this code runs?

Java
package pack1;

public class Parent {
    protected String message = "Hello from Parent";
}

package pack2;

import pack1.Parent;

public class NonChild {
    public void printMessage() {
        Parent p = new Parent();
        System.out.println(p.message);
    }
}

package pack2;

public class Test {
    public static void main(String[] args) {
        NonChild nc = new NonChild();
        nc.printMessage();
    }
}
ANo output
BCompilation error: message has protected access in Parent
CRuntime error: NullPointerException
DHello from Parent
Attempts:
2 left
💻 code output
advanced
2:00remaining
Accessing protected member via object reference of superclass

What is the output of this code?

Java
package pack1;

public class Parent {
    protected String message = "Hello from Parent";
}

package pack1;

public class Child extends Parent {
    public void printMessage() {
        Parent p = new Parent();
        System.out.println(p.message);
    }
}

package pack1;

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.printMessage();
    }
}
ACompilation error: message has protected access in Parent
BRuntime error: NullPointerException
CNo output
DHello from Parent
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
Protected access modifier and package boundaries

Which statement about the protected access modifier in Java is TRUE?

AA protected member is accessible only within its own package and not by subclasses in other packages.
BA protected member is accessible by any class in any package without restriction.
CA protected member is accessible within its own package and also by subclasses even if they are in different packages.
DA protected member is accessible only by subclasses in the same package, but not in different packages.
Attempts:
2 left