In Java, can a subclass access protected members (fields and methods) of its superclass?
A. Only if the subclass is marked as "final"
B. No, protected members are not accessible to subclasses
C. Yes, as long as it's in the same package
D. Yes, regardless of the package
Answer: Option D
Solution (By Examveda Team)
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.
Join The Discussion
Comments (2)
A. The process of acquiring properties and behaviors of one class by another
B. The process of creating objects
C. The process of encapsulation
D. The process of overloading methods
In Java, which keyword is used to implement inheritance between classes?
A. inheritsFrom
B. inherits
C. implements
D. extends
A. The class that inherits properties and behaviors
B. The child class
C. The class that is inherited from
D. The class that is marked as "final"
In Java, can a subclass inherit constructors from its superclass?
A. Only if the subclass is marked as "final"
B. Yes, a subclass inherits constructors from its superclass
C. Only if the superclass is marked as "static"
D. None of the above

Wrong Answer. Subclasses can access the protected members of their superclass regardless of whether they are in the same package or a different one.
why? subclass can access protected members of its superclass even in a different package