Question 21Asked in DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)

class Parent { protected void protectedMethod() {

class Parent { protected void protectedMethod() { System.out.println("Protected method in Parent"); } } class Child extends Parent { public void childMethod() { protectedMethod(); } } public class Test { public static void main(String[] args) { Child child = new Child(); child.childMethod(); } } What will happen when the code is executed?
  1. AThe code will compile and print 'Protected method in Parent'.
  2. BCompilation Error: The protectedMethod is not visible in the Child class.
  3. CCompilation Error: childMethod is not visible due to access restrictions.
  4. DRuntime Error: Cannot access protectedMethod from the Child class.
Show answerHide answer

Correct answer: (A) The code will compile and print 'Protected method in Parent'.

Explanation

A protected member is accessible in a subclass, so Child can call protectedMethod() directly. The code compiles and prints "Protected method in Parent".

From DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 1 (9:00 AM)

This is question 21 of DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 1 (9:00 AM). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.