What are Java's four access modifiers, and where exactly is protected accessible?
Short answer
From narrow to wide: private, default (same package), protected (adds subclasses in other packages), public. The trap: a subclass in another package uses protected members only through a reference of its own type.
3 points to say:
private< default <protected<public.protectedis also open to the same package.- Fields are always
private.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.