[!INDEX]

  1. File Structure
  2. Protected_AM
  3. Default_AM
  4. Private_AM
  5. Public_AM

1. File Structure

[!NOTE] src

  1. package1
  1. package2

2. Protected-Code

// parent.java
package package1;  
public class Parent {     
    protected String familyName = "Smith"; // Protected member      
        protected void displayFamilyName() {    // Protected method
            System.out.println("Family Name: " + familyName);     
        } 
}

//Sibling.java
package package1;
public class Sibling {
    public void showFamily(Parent parent) { // without Parent's object
        // Accessing the protected member directly
        System.out.println("Sibling's Family Name: " + parent.familyName); // This works
        parent.displayFamilyName(); // This also works
    }
}

//Child.java
package package1;
public class Child extends Parent {
    public void showFamily() {
        System.out.println("Child's Family Name: " + familyName); // Accessing protected member
        displayFamilyName(); // Accessing protected method
    }
}

// Main.java
package package2;
import package1.Child;
import package1.Parent;
import package1.Sibling;

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.showFamily(); // This works

        Sibling sibling = new Sibling();
        Parent parent = new Parent();
        sibling.showFamily(parent); // This works because Sibling is in the same package as Parent
    }
}

[!NOTE]

Summary of Access

  1. Sibling Accessing Parent:

    • class does not need to be a subclass to access protected members from another class in the same package.
  2. Child Accessing Parent:

    • Child can also access familyName and displayFamilyName() directly because it is a subclass of Parent.
  3. Main Class:

    • In Main, while it can create an instance of Child and access protected members through inheritance, it cannot access familyName or displayFamilyName() directly from Parent since it's in a different package.

4. Default_AM-Code

// Parent.java
package package1;
class Parent { // Default access modifier
    String familyName = "Smith"; // Default access modifier for field

    void displayFamilyName() { // Default access modifier for method
        System.out.println("Family Name: " + familyName);
    }
}

//Sibling.java
package package1;
public class Sibling {
    public void showFamily() {
        Parent parent = new Parent(); // Creating an instance of Parent
        System.out.println("Sibling's Family Name: " + parent.familyName); // Accessing default member
        parent.displayFamilyName(); // Accessing default method
    }
}

//Child.java
package package1;
public class Child {
    public void showFamily() {
        Parent parent = new Parent(); // Creating an instance of Parent
        System.out.println("Child's Family Name: " + parent.familyName); // Accessing default member
        parent.displayFamilyName(); // Accessing default method
    }
}

//Main.java
package package2;
import package1.Sibling; // Importing Sibling
public class Main {
    public static void main(String[] args) {
        Sibling sibling = new Sibling();
        sibling.showFamily(); // This works because Sibling is in the same package
        // The following line would NOT compile:
        // Parent parent = new Parent(); // Error: Parent has default access, cannot be accessed in a different package
    }
}

7. Default_AM-summary

[!NOTE]

  1. Use default access when you want to limit visibility to classes within the same package.
  2. allows access only within classes in the same package. not from classes in different packages.

3. Private_AM-Code

//Parent.java
package package1;
public class Parent {
    private String familyName = "Smith"; // Private field

    private void displayFamilyName() { // Private method
        System.out.println("Family Name: " + familyName);
    }

    // Public method to demonstrate private member access within the same class
    public void accessPrivateMethod() {
        System.out.println("Accessing private members within the same class:");
        displayFamilyName(); // This works, since it's within the same class
    }
}

//Sibling.java
package package1;
public class Sibling {
    public void showFamily() {
        Parent parent = new Parent();
        System.out.println("Sibling's Family Name: " + parent.familyName); // Error: Cannot access private field
        parent.displayFamilyName(); // Error: Cannot access private method
        // Can only use the public method to access private members
        parent.accessPrivateMethod(); // This works, as accessPrivateMethod() is public
    }
}

//Child.java
package package1;
public class Child extends Parent {
    public void showFamily() {
        // Cannot access private members of Parent, even though Child extends Parent
        System.out.println("Child's Family Name: " + familyName); // Error: Cannot access private field
        displayFamilyName(); // Error: Cannot access private method
        accessPrivateMethod(); // This works because accessPrivateMethod() is public
    }
}

//Main.java
package package2;
import package1.Sibling;
import package1.Child;
public class Main {
    public static void main(String[] args) {
        Sibling sibling = new Sibling();
        sibling.showFamily(); // This works, but can't access private members directly

        Child child = new Child();
        child.showFamily(); // This works, but private members are still inaccessible directly
    }
}

7. Private_AM-Summary

[!NOTE]


2. Public_AM-Code

//Parent.java
package package1;
public class Parent { // Public class, accessible from any package
    public String familyName = "Smith"; // Public field

    public void displayFamilyName() { // Public method
        System.out.println("Family Name: " + familyName);
    }
}

//Sibling.java
package package1;
public class Sibling {
    public void showFamily() {
        Parent parent = new Parent(); // Can create an instance of Parent

        System.out.println("Sibling's Family Name: " + parent.familyName); // Accessing public field
        parent.displayFamilyName(); // Accessing public method
    }
}

//Child.java
package package1;
public class Child extends Parent {
    public void showFamily() {
        System.out.println("Child's Family Name: " + familyName); // Accessing public field from Parent
        displayFamilyName(); // Accessing public method from Parent
    }
}

//Main.java
package package2;
import package1.Parent;
import package1.Sibling;
import package1.Child;
public class Main {
    public static void main(String[] args) {
        Parent parent = new Parent();
        System.out.println("Main's Family Name: " + parent.familyName); // Accessing public field
        parent.displayFamilyName(); // Accessing public method

        Sibling sibling = new Sibling();
        sibling.showFamily(); // This works because everything in Parent is public

        Child child = new Child();
        child.showFamily(); // This works because everything in Parent is public
    }
}

7. Public_AM-Summary

[!NOTE]

  1. accessible from anywhere** in the program, across packages and classes.
  2. no restriction
  3. can be used in any file where the class is imported.
  4. Use the public modifier for libraries, APIs, or utility methods .