Engineering Full Stack Apps with Java and JavaScript
Access modifiers are used to specify the accessibility or access levels of a type (class, interface) and its members (methods, variables and even constructors). There are three access modifiers and four access levels in Java. The three access modifiers are are private, protected and public. Four access levels (from most restricted to least restricted) are private, default (no modifier), protected and public. If no access modifier is specified, it is called a default access level.
Access levels and their accessibility can be summarized as:
Private: Same class.
Default: Same class, Same package.
Protected: Same class, Same package, Subclasses.
Public: Same class, Same package, Subclasses, Everyone.
The top level classes (classes not within another class) have only public and default access; but for inner classes (classes within classes) have all four access levels.
Private members are accessible only within the same class and also are not inherited.
Default (no modifier) members can be accessed by members of the same class and members of any class in the same package.
Default members are inherited by another class only if both parent and child are in same package.
Protected members can be accessed by
members of the same class
members of any class in the same package (same as default)
subclasses (any level of subclass hierarchy) in other packages (only through inheritance).
Protected members (static or instance) cannot be accessed from a non-subclass in another package.
Inherited protected members in the child class cannot be accessed using a parent reference variable (irrespective of the object it point to at runtime).
Static protected members can be accessed in subclasses through:
object reference (Parent or Child) in subclasses
class name
Public members can be accessed from everywhere within your application either through inheritance or through object reference.
Note that the class or packages should also be accessible to access its members. You will not be able to access a public member from a default class from another package.
Question 1
class MyClass{
private int var1;
int var2;
protected int var3;
public int var4;
}
}
Consider another class AnotherClass in the same package:
class AnotherClass{
public static void main(String[] args)
{
MyClass c = new MyClass();
System.out.println(c.var1);
System.out.println(c.var2);
System.out.println(c.var3);
System.out.println(c.var4);
}
}
}
Which all lines are valid and which all are invalid?
Question 2
In Question 1, if AnotherClass was in different package, which all lines are valid and which all are invalid?
Question 3
In Question 1, if AnotherClass was a subclass in different package (AnotherClass extends MyClass), which all lines are valid and which all are invalid?
Question 4
In Question 1, add a protected access modifier to MyClass as:
protected class MyClass{…
and if AnotherClass was a subclass in different package (AnotherClass extends MyClass), which all lines are valid and which all are invalid?
Question 5:
I have a class Parent in one package and class 'Child extends Parent' in another package. From Child can I create an object of Parent inside my Child and access the protected variable through that reference?
Eg. Parent p = new Parent(); and the accessing as p.myProtectedVariable.
Question 6:
Can I access a static protected variable through object reference from a non subclass within Child’s package?
Question 7:
Can I create an object of the child from a non subclass in child’s package and access inherited instance protected variable through that object reference?
Answer 1
Invalid:
System.out.println(c.var1);
Private variables cannot be accessed from other classes.
Valid:
System.out.println(c.var2);
System.out.println(c.var3);
System.out.println(c.var4);
Answer 2
Invalid:
System.out.println(c.var1);
Private variables cannot be accessed from other classes.
System.out.println(c.var2);
Default variables cannot be accessed from other packages.
System.out.println(c.var3);
Protected variables cannot be accessed from non-subclasses in other packages.
Valid:
System.out.println(c.var4);
Answer 3
Compilation of the class itself will fail as MyClass cannot be extended from another package as MyClass is default.
Answer 4
Compilation will fail with an error: Illegal modifier for the class MyABCClass; only public, abstract & final are permitted.
Remember that top level classes (classes not within another class) have only public and default access.
Answer 5:
No, we won't be able to access the protected variable using a Parent reference inside Child in a different package. Protected variables can be accessed by subclasses in another package only through inheritance. However static variables don’t have this limitation. Static protected variables can also be accessed through Parent reference in a subclass of another package.
Answer 6:
No. Protected variables (static or instance) cannot be accessed from a non-subclass in another package.
Answer 7:
No. Protected variables (static or instance) cannot be accessed from a non-subclass in another package. Protected variables act similar to private to the subclass in another package and hence cannot be accessed from another class even from the child package.
You can find a complete example covering all access levels from different regions of your code @ http://www.javajee.com/explaining-access-modifiers-with-example-code.