Access modifier interview questions, with answers
Access modifiers are four keywords and one table, and the table is where candidates slip: what protected means from another package, what a member with no modifier at all can see, whether a top-level class can be private. Interviewers ask these because the answers are exact — there is no partial credit for "kind of visible".
Here is the table and the questions built on it, with Java as the reference and C++ noted where it differs. Then take the free OOP diagnostic — ten questions across all fifteen OOP topics, with the weak ones named.
The questions, with answers
1.What are the four access levels in Java, and who can see each?
From most open to most closed. public: any class anywhere. protected: the same package, plus subclasses in other packages. Default, also called package-private, which is what you get with no keyword: the same package only, subclass or not. private: the declaring class only — not subclasses, not the same package. The two that trip people up are the middle ones: protected is wider than default, not narrower, because it adds subclasses in other packages to everything default already allows; and private really does mean the class, so a subclass in the same file still cannot touch a private field of its parent. Learn the table as four rows and three columns — same class, same package, subclass elsewhere, everywhere — and fill it in from memory.
2.How does protected behave across packages, and what is the subtlety?
A protected member is visible to any class in the same package and to subclasses in other packages — but a subclass in another package may only access it through a reference of its own type, not through a reference typed as the parent. Inside class Dog (package zoo) that extends Animal (package base), this.sound and new Dog().sound work, but new Animal().sound does not compile, even though sound is protected in Animal. The rule exists so that a subclass can reach its own inherited state without gaining access to every other Animal's. It is the single most-asked protected question, and the answer "protected means subclasses can access it" is marked incomplete without the through-your-own-type caveat.
package base; public class Animal { protected String sound = "..."; } package zoo; import base.Animal; public class Dog extends Animal { void test(Animal other, Dog pup) { String a = this.sound; // fine: own inherited member String b = pup.sound; // fine: reference of the subclass type // String c = other.sound; // does not compile: reference typed as Animal } }3.What happens when a class has no access modifier, and can a top-level class be private?
A top-level class can be only public or package-private: with no modifier it is visible to its own package and invisible outside it, which is how you hide implementation classes from other packages without any extra machinery. private and protected are compile errors on a top-level class — private would make it visible to nothing, and protected has no meaning without an enclosing class. Nested classes are different: a member class can take any of the four levels, and a private nested class is a common way to keep a helper, an iterator or a node type entirely inside the class that uses it. One public class per file, named after the file, is the related rule interviewers often bundle in.
4.Are private members inherited, and how can a subclass reach them?
A private field or method is part of every subclass object in memory — the parent's constructor initialises it, and the parent's public methods use it — but the subclass cannot name it: no direct access, no override, no reflection-free way in. Whether that counts as "inherited" is a definitional argument; the Java Language Specification says private members are not inherited, and the practical answer is that the subclass reaches them only through the parent's non-private methods, typically a protected or public getter. A private method with the same signature in a subclass is not an override but an unrelated method, so @Override on it fails, and dynamic dispatch never chooses it. The design lesson: mark a field protected only when subclasses genuinely need to touch it, since protected exposes it to the whole package too.
5.What is the difference between access modifiers and non-access modifiers?
Access modifiers — public, protected, default, private — control who can see a member. Non-access modifiers change what it is: static (belongs to the class), final (cannot be reassigned, overridden or extended), abstract (must be implemented by a subclass), synchronized (one thread at a time), transient (skipped by serialisation), volatile (reads and writes go straight to main memory), native (implemented outside Java). The two groups combine freely, so a method can be public static final. Interviewers use this to check that you do not answer "static" when asked for an access modifier, and to lead into which combinations are illegal — abstract with final or private, for instance, since an abstract method must be overridable.
6.Can an overriding method have a different access level from the method it overrides?
It can be the same or wider, never narrower. A public method in the parent must stay public in the child; a protected one can become public; a package-private one can become protected or public. Narrowing is a compile error because it would break substitution — code holding a parent reference expects to call the public method, and a private override would have to refuse it. Private methods are not overridden at all, and a public interface method must be implemented as public, which is the error people hit when they omit the keyword in an implementing class (default access is narrower than public). The same direction rule applies to C# and to C++ virtual functions in spirit, though C++ does not enforce it.
7.How do access specifiers work in C++, and what do private inheritance and friend do?
C++ has public, protected and private with the same meaning for members as Java, with no package level, and it adds two things Java lacks. The inheritance itself has an access specifier: class Derived : public Base keeps the base's public members public, protected inheritance makes them protected, and private inheritance makes every inherited member private in Derived — callable from inside Derived's own methods but not on a Derived object from outside, and not by further subclasses. Private inheritance models "implemented in terms of", and most style guides prefer composition for that. friend lets a named function or class access private members, which is how operator<< prints a class's internals; it breaks encapsulation deliberately and locally, and the interviewer wants you to say so.
8.How do you choose the right access level when designing a class?
Start as closed as possible and open only what someone needs: fields private by default, methods private unless they are part of the class's contract, and the contract public. Use protected sparingly, for hooks a subclass is meant to override or state it genuinely needs, remembering that it also opens the member to the package. Use package-private for classes and helpers that exist only to support one package, which keeps a library's public surface small and lets you change internals without breaking users. The principle behind all of it is that anything public is a promise — you cannot remove or change it without breaking callers — so every modifier you widen is a commitment you are making on behalf of every future version of the code.
How the diagnostic asks it
One question from the OOP bank, exactly as a sitting would show it. The bank has 4 on access modifiers and 60 across OOP.
In Java, a member declared as 'private' in a class can be accessed from where?
- 1From any subclass, regardless of package.
- 2From any class in the program.
- 3From any class in the same package.
- 4Only from within the same class in which it is declared.correct
A private member is accessible only inside the declaring class itself, not even by subclasses or classes in the same package. Access from any class in the same package describes default (package-private) access; access from any subclass regardless of package describes protected access; and access from any class in the program describes public access.
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 OOP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.