Java language uses access modifiers to set the level of the visibility of a class to another class. A class has access to another class if it can:
- Initialize that class
- Extend that class
- Access methods and instance variables defined in that class
If a class Class1 has access to the class Class2, Class2 is visible to Class1. In the same way, if a method or instance variable of Class2 can be accessed by Class1, then this method or instance variable is said to be visible to Class1.
A class has access to all the methods and variables that it defines.
Now let’s see the rules of class access in Java.
For classes:
1- A public class is visible to all classes. It can be initialized by any class if it has a public constructor.
2- A protected class is visible to its subclasses and the other classes define in the same package.
A class cannot be declared private.
A class A’s methods and variables can be visible to class B only if the class A is visible to class B.
For methods and variables:
1- Public instance variables and methods are visible to any other class.
2- Protected instance variables and methods are visible to only classes inside the same package and to the subclasses.
3- Instance variables and methods with default access are visible to only the classes inside the same package.
4- Private methods and fields can be accessed only by methods inside the same class, including the main method.
For class A’s x method to be visible to class B
1- Class A should have access to class B
2- Method x should be visible to class B
Now let’s see this with examples.
package com.mycompany;
public class Person
{
public String firstName;
String middleName;
protected String lastName;
private int age;
public void sayName(){
System.out.println(“My name is + firstName + “ “ + lastName);
}
private void sayAge(){
System.out.println(“I am “ + age + “ years old.”);
}
}
package com.mycompany;
//PersonInfo class is defined inside the same package.
public class PersonInfo {
public static void main(String [] args) {
Person person = new Person();
person.firstName = “James”; // public instance variable is accessible by all the other classes.
person.middleName = “Stanley”; // instance variable with default access is accessible by the classes inside the same package.
person.lastName = “West”;//protected instance variable is accesible by the classes inside the same package.
//This would cause a compile error because age is private.
//person.age = 48;
//This would again cause a compile error because age is private and not visible to this class.
//System.out.println(p.firstName + “ “ + p.lastName + “ is “ + p.age + “ years old.”);
}
package com.mycompany.examples;
//PersonInfo class is defined inside the same package.
public class Example {
public static void main(String [] args) {
Person person = new Person();
person.firstName = “James”; // public instance variable is accessible by all the other classes.
//This would cause a compile error since instance variable with default access is not accessible by the classes outside the same package.
//person.middleName = “Stanley”;
//This would cause a compile error since protected instance variable is not accessible by the classes outside the same package.
//person.lastName = “West”;
//This would cause a compile error because age is private.
//person.age = 48;
//This would again cause a compile error because age is private and not visible to this class.
//System.out.println(p.firstName + “ “ + p.lastName + “ is “ + p.age + “ years old.”);
}
package com.mycompany.examples;
public SuperClass extends Person {
public static void main(String [] args) {
Person person = new Person();
person.firstName = “James”; // public instance variable is accessible by all the other classes.
//This would cause a compile error since instance variable with default access is not accessible by child classes.
//person.middleName = “Stanley”;
//This would cause a compile error since protected instance variable is not accessible by the subclasses.
//person.lastName = “West”;
//This would cause a compile error because age is private.
//person.age = 48;
//This would again cause a compile error because age is private and not visible to this class.
//System.out.println(p.firstName + “ “ + p.lastName + “ is “ + p.age + “ years old.”);
}
}
Final classes
final is one of the three modifiers allowed in class declarations along with public and protected. A final class cannot be extended. In other words, a final class does not have subclasses. A final class can extend other classes.
public class MainClass {
}
public final class FinalClass extends MainClass{
}
//This would cause a compile error since final classes cannot be extended.
//public SubClass extends FinalClass {
//}
For example, String class in Java is a final class and cannot be extended.