Study Notes on Access Level Modifiers in Java - q 5 & 6

Overview of Access Level Modifiers in Java

In this tutorial, John from Cave of Programming.com discusses the various access level modifiers in Java, specifically focusing on public, private, and protected. These modifiers control the visibility and accessibility of class members.

Public Modifier

  • Definition:

    • The public access modifier allows class members (variables, methods) to be accessible from anywhere in the application.

  • Example:

    • Creation of a class named Plant:

    • Declaration of a public instance member variable:

      public class Plant {
          public String name;
      }
    
    • Accessing the public variable:

      Plant plant = new Plant();
      plant.name = "Freddie";
    
  • Best Practices:

    • While using public member variables is possible, it is often considered bad practice. Developers are encouraged to encapsulate data and provide access through methods to maintain control over data manipulation.

    • If public variables are declared, it is usually for constants, commonly defined as:
      java public static final int ID = 8;

    • Importance of static and final:

    • static means the variable belongs to the class, not instances.

    • final indicates that the variable cannot be changed after initialization.

Private Modifier

  • Definition:

    • The private access modifier restricts visibility to the class where it is declared. Members marked as private cannot be accessed from outside the class, even by subclasses.

  • Example:

    • In the Plant class, a private member variable can be declared as follows:

    private String type;
    
    • Attempting to access a private variable from a subclass (e.g., Oak which extends Plant):

    Oak oak = new Oak();
    oak.type = "tree"; // This will result in an error
    
  • Scope Clarification:

    • The scope of a private variable only includes methods defined within the class that declares it.

    • Example of valid access within the class:
      java this.type = "plant";

Protected Modifier

  • Definition:

    • The protected access modifier allows members to be accessed from within the same class, in subclasses regardless of package, and within the same package.

  • Example:

    • Declaration of a protected member in Plant:

    protected String size;
    
    • Valid access in the Oak subclass:

    this.size = "large";
    
  • Package-Level Access:

    • Members declared as protected can be accessed by any class in the same package.

    • In a new class named Field, which is in the same package as Plant, protected members can be accessed without issue:
      java Plant plant = new Plant(); System.out.println(plant.size);

Default (Package-Level) Access

  • Definition:

    • If no access modifier is specified, the member has package-level visibility. This means it can only be accessed by classes within the same package, but not from outside packages.

  • Example:

    • Declaring height without an access modifier:

    int height;
    
    • Accessing height from the same class or subclass within the same package is valid:

    this.height = 10;
    
  • Implications of Package-Level Access:

    • If a subclass is defined in a different package, it cannot access the package-visible member:
      java System.out.println(this.height); // This won't work

Summary of Access Level Modifiers

  • Public:

    • Accessible from anywhere in the application.

  • Private:

    • Accessible only within the same class.

  • Protected:

    • Accessible within the same class, subclasses, and the same package.

  • Default (No Modifier):

    • Accessible only within the same package.

Class Access Modifiers

  • Classes can also have access modifiers, but there are limitations:

    • A public class is accessible from outside its package and must share its name with the file name.

    • A Java file can have multiple classes but only one public class.

    • Classes cannot be declared as private or protected.

  • Package-Private Class:

    • If no access modifier is specified, the class is only visible to classes in the same package.

Conclusion

These distinctions between access modifiers provide structure to Java programs, allowing developers to control the scope of their variables and methods. Understanding how to appropriately use public, private, protected, and package-level access is essential for writing robust and maintainable code. The tutorial concludes with an invitation for viewers to explore further tutorials on the website.