var in Java 11 bypass the protected access restriction

The behavior of var in Java, introduced in Java 10, does not bypass the protected access restriction in any special way. Instead, the misunderstanding often arises from how var is interpreted and its interactions with visibility rules in Java.

Let’s break it down:


How var Works

  1. Type Inference:
    • var is a shorthand for declaring local variables with their types inferred by the compiler at compile time.The type of the variable is determined from the initializer expression.
    var str = "Hello, World!";
    // Equivalent to
    String str = "Hello, World!";
  2. Scope:
    • var can only be used for local variables within methods, constructors, or initialization blocks.
    • It cannot be used for instance variables, method parameters, or return types.

The Protected Access Modifier

  1. Rules:
    • protected members in Java are accessible:
      • Within the same package.
      • In subclasses of the class where the member is declared (even if the subclass is in a different package).
    • The access in subclasses can occur only via inheritance (e.g., calling the protected member directly on this or a subclass instance).
  2. Why This Restriction Exists:
    • protected restricts access to ensure encapsulation while allowing controlled inheritance-based access.

Misinterpretation of var and protected

Scenario Leading to Confusion

Consider the following example:

package com.example.base;

public class Parent {
protected String protectedField = "Protected Value";
}
package com.example.child;

import com.example.base.Parent;

public class Child extends Parent {
public void accessProtected() {
var value = this.protectedField; // Works
System.out.println(value);
}
}

Explanation

  1. Why Does This Work?
    • protectedField is protected and accessible in Child because Child inherits from Parent.
    • The var keyword simply infers the type of protectedField as String during compilation. This inference does not bypass the protected access rules.
  2. Does var Change Access Rules?
    • No, var does not bypass access rules. If the access to protectedField is invalid (e.g., from a non-subclass or unrelated class), it would result in a compilation error.

Scenario Where Access Fails

package com.example.unrelated;

import com.example.base.Parent;

public class Unrelated {
public void accessProtected() {
Parent parent = new Parent();
var value = parent.protectedField; // Compilation error!
}
}

Why Does This Fail?

  • protectedField is not accessible because Unrelated is neither in the same package as Parent nor a subclass of Parent.

Key Takeaways

  1. var does not bypass protected or any other access restrictions. It merely infers the type of a variable at compile time.
  2. Any perceived bypass is likely due to misunderstanding the rules of protected or the fact that var allows concise syntax that might make access appear different.
  3. The rules for protected remain consistent regardless of whether var is used or explicit types are declared.

No images available.