ExamVeda
Login
Home
This question belongs to Java Program Inheritence
Inheritence
?

In Java, can a subclass access protected members (fields and methods) of its superclass?

Answer & Solution
Correct Answer: Option D
In Java, protected members (fields and methods) of a superclass are accessible to its subclasses.

Unlike private members, which are inaccessible outside the class, protected members can be accessed:

1. Within the same package: Any class within the same package can access protected members.

2. In subclasses (even if in a different package): A subclass can access protected members of its superclass, even if they are in different packages, provided they are accessed through inheritance (i.e., using super or via an instance of the subclass).

This ensures that subclasses get access to important superclass members while still maintaining some level of encapsulation.
Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Zhida Wang
Zhida Wang 2 years ago
Wrong Answer. Subclasses can access the protected members of their superclass regardless of whether they are in the same package or a different one.
Kareem Abdullah
Kareem Abdullah 3 years ago
why? subclass can access protected members of its superclass even in a different package