Challenge - 5 Problems
Protected Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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(); } }
Attempts:
2 left
💻 code output
intermediate2: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(); } }
Attempts:
2 left
💻 code output
advanced2: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(); } }
Attempts:
2 left
💻 code output
advanced2: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(); } }
Attempts:
2 left
🧠 conceptual
expert2:00remaining
Protected access modifier and package boundaries
Which statement about the protected access modifier in Java is TRUE?
Attempts:
2 left
