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 *