Examveda

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.

This Question Belongs to Java Program >> Inheritence

Join The Discussion

Comments (2)

  1. Zhida Wang
    Zhida Wang:
    7 months 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.

  2. Kareem Abdullah
    Kareem Abdullah:
    2 years ago

    why? subclass can access protected members of its superclass even in a different package

Related Questions on Inheritence

What is inheritance in Java?

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