Class members and their "visibility"

  • Review:

      • Class member = a variable or a method defined in a class

        public class MyClass
        {
            private int x;             // class member
            public  int get() { ... }  // class member
        }
        

  • Visibility modifiers can be used to specify the visibility (I like to called it accessibility) of a class and its members

  • The visibility (accessibility) modifiers of Java are:

       Already discussed: 
          public:      accessible from any class
          private:     accessible from methods in same class 
      
       New: 
          [empty]:     accessible from methods in same package   
          protected:   will be discussed later 

The effect of public and private visibility (access) qualifiers

private members can not be accessed from outside the class:

The effect of public and private visibility (access) qualifiers

private members can not be accessed from outside the class:   demo program

public class C1
{
    public  int x1;
    private int x2;

    public  void m1() {}
    private void m2() {}
}


public class myProg { public static void main(String[] args) { C1 o = new C1(); // Make a C1 object o.x1 = 1; // Access a public variable o.x2 = 1; // Access a private variable compile error o.m1(); // Invokes a public method o.m2(); // Invokes a private method compile error } }

DEMO: demo/03-classes/17-private   (too easy, don't show..., compile it yourself.)

Why do programming languages provide different accessibilities

  • A large computer program has many methods stored in different classes

  • Methods defined inside a class are used to solve the same problem

  • Methods defined inside different classes usually do not solve the same problem

  • A common cause for errors is accidental update of variable(s) by a method in a different class:

  • Limiting access to variables (with private) will reduce accidental update of variable(s) by a method from another class !!!

Java packages

  • Some problems are too complex that they cannot be solved by methods inside one single class

  • We may need to write multiple classes to solve such complex problems

    • Programs will run more efficiently if they can access variables directly...

      I.e.: Getter (e.g.: getRadius())) and setter (e.g.: setRadius()) will slow down a program !

  • Package in Java is used to organize multiple classes that coorperate and perform similar tasks

  • The facilitate (= make it easier) the coorperation:

      • Java can allow methods defined in classes inside the same package to access each others members (variables and methods) directly


  • Before we learn this accessibility modifier, I need to explain how to create packages in Java (with BleuJ)....

How to create a package in BlueJ

  • Let's make a new project in BlueJ:

      Click on Project 
    
                        Name:     LearnPackage
    		    Location: C:\Java\BlueJ


  • Open a File Browser and go to C:\Java\BlueJ to see what happens

  • To make a package p1 inside the LearnPackage project:

      Click on Edit --> New Package
    
                        Package Name: p1

    Classes in package p1 will be stored inside a folder named p1 on the computer !

How to create a class inside a package p1 in BlueJ

  • You can open the package p1 by:

      Double click on package p1 in LearnPackage 

    A new window for LearnPackage [p1] will open


  • Open a File Browser and go to C:\Java\BlueJ\LearnPackage\p1 to see what happens

  • To make a class C1 inside the LearnPackage.p1 package:

      Click on New Class:
    
                        Class Name: C1

    Classes in package p1 will be stored inside a folder named p1 on the computer !

Example of a class inside the package p1

Use BlueJ and create this new class inside package p1:

package p1;   // Inserted by BlueJ !

public class C1
{
    public  int x1;
            int x2;         // Default (= package) access modifier
    private int x3;
    
    public  void m1() {}
            void m2() {}    // Default (= package) access modifier
    private void m3() {}
}
  

The members x2 and m2() has no access modifier
In that case, they will be assigned with the default access modifier

The default access modifier

  • If a member (= variable or method) is not assigned with a modifier:                                                        

    The member will receive the "default" access modifier

The default access modifier

  • A member with the default access can be accessed from methods in classes belong to the same package:

    Remember:   classes in the same package coorperate to perform a task !

DEMO: demo/03-classes/18-package/p1/Demo.java + C1.java (use javac -cp ..)

The default access modifier

  • A member with the default access is not accessible from methods in classes belong to a different package:

    Remember:   classes in different packages do not coorperate to perform a task !

DEMO: demo/03-classes/18-package/Demo.java + p1/C1.java

Summary

Members with the default access modifier can be accessed from methods in classes inside the same package: