Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Packages are like folders on your computer — they organize related classes together. Access modifiers are like door locks — they control who can enter (access) a class or its members.
Packages
java
QUBITS OF DPK
Package Naming Convention
javascript
QUBITS OF DPK
Package = Folder Structure
javascript
QUBITS OF DPK
Why Packages?
javascript
QUBITS OF DPK
Access Modifiers
java
QUBITS OF DPK
Access Modifier Use Cases
java
QUBITS OF DPK
️ Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Packages organize related classes and prevent naming conflicts, following reverse domain naming convention. Access modifiers control visibility: private (class only), default (package), protected (package + subclasses), public (everywhere). In encapsulation, fields are private with public getters/setters. Protected is used for template method patterns where parent wants child to access but not external code. You cannot reduce access modifier visibility when overriding — can only maintain or increase."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between protected and default access?
Default (package-private): accessible only within the same package — subclasses in DIFFERENT packages cannot access it. Protected: accessible within same package AND in subclasses anywhere (even different packages). Example: if Vehicle.engineType is protected, a Car in a different package that extends Vehicle can access engineType. If default, it can't. In practice: protected for members designed for inheritance across packages; default for package-internal implementation details.
Q2. Can you reduce access modifier visibility when overriding?
No — you can only maintain or increase visibility. If parent method is protected, override can be protected or public, but NOT private or default. Reason: the Liskov Substitution Principle — child must be usable wherever parent is used. If parent's method is accessible, child's override must be at least as accessible. public can only be overridden as public. Reducing visibility would break callers using parent reference.
Q3. What is a package and why is it used?
A package is a namespace that groups related classes and interfaces. Uses: (1) Organization — com.deepak.banking.service vs com.deepak.banking.model. (2) Naming conflict prevention — two classes named User can coexist in com.google.User and com.amazon.User. (3) Access control — default access restricts to same package. (4) Distribution — packages are deployed as JARs. Convention: reverse domain name (com.companyname.project.module).
Q4. What happens if two classes in different packages have the same name?
No conflict — they are treated as completely different classes because their fully qualified names differ: com.google.User vs com.amazon.User. When both are imported in the same file, Java shows ambiguity error — fix by using fully qualified name: com.google.User googleUser = new com.google.User(). This is exactly why package naming (reverse domain) exists — globally unique namespacing.
Q5. Can a private method be overridden?
No. Private methods are not visible outside the class — they're not inherited, so they cannot be overridden. If a child class defines a method with the same name and signature as a parent's private method, it's a completely new method (method hiding), not an override. @Override annotation would cause a compile error. Calling the method on a parent reference always invokes the parent's private method, regardless of the actual object type — no polymorphism.