How will you access default and protected class?

🔐 Access Modifiers Overview in Java

Java provides four levels of access control:

ModifierSame ClassSame PackageSubclass (any pkg)Other Packages
public
default (no modifier)
protected
private

📘 Default Access (Package-Private)

✔️ Meaning:

  • No modifier means default access.
  • The class or member is accessible only within the same package.

📦 Example:

// File: com/example/Vehicle.java
package com.example;

class Vehicle {  // default access
    void move() {
        System.out.println("Vehicle is moving");
    }
}
// File: com/example/Test.java
package com.example;

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Vehicle();  // ✅ Accessible (same package)
        v.move();
    }
}
// File: com/other/Test2.java
package com.other;

import com.example.Vehicle;

public class Test2 {
    public static void main(String[] args) {
        Vehicle v = new Vehicle();  // ❌ Compile error (different package)
    }
}

🔒 Protected Access

✔️ Meaning:

  • Accessible in the same package
  • Also accessible in subclasses, even in different packages (through inheritance only)

📦 Example:

// File: com/example/Animal.java
package com.example;

public class Animal {
    protected void sound() {
        System.out.println("Animal sound");
    }
}
// File: com/other/Dog.java
package com.other;

import com.example.Animal;

public class Dog extends Animal {
    public void bark() {
        sound();  // ✅ Allowed because Dog extends Animal
    }
}
// File: com/other/TestDog.java
package com.other;

import com.example.Animal;

public class TestDog {
    public static void main(String[] args) {
        Animal a = new Animal();
        a.sound();  // ❌ Not allowed: protected method via reference
    }
}

📌 To access a protected method in a subclass outside the package, you must call it from inside that subclass—not via object reference.

🎯 Interview Tips & Common Questions

📖 Common Java Interview Questions

  1. Q: What’s the difference between default and protected access?
    • A: Default allows access within the same package only. Protected allows access within the same package + from subclasses outside the package (via inheritance).
  2. Q: Can a protected class be accessed from a different package?
    • A: A class cannot be declared protected. Only class members (fields and methods) can be protected.
  3. Q: How do protected members behave in subclass vs object reference?
    • A: In a subclass, you can access protected members directly. But from an object of superclass, you cannot access protected members outside the package.
  4. Q: Can a top-level class have default or protected access?
  • A: A top-level class can be public or default only. protected is not allowed for top-level classes.

🧠 Key Takeaways

  • 🟢 Use default when your class is only used internally in the same package.
  • 🟢 Use protected to expose members to subclasses across packages.
  • protected class is invalid for top-level classes.
  • ⚠️ Be careful when accessing protected methods—it must be through inheritance in other packages.

Leave a Reply

Your email address will not be published. Required fields are marked *