Review:    accessibility modifiers

  • Accessibility modifiers specify where a variable/method can be used:

      public void main(String[] args) ...
    

  • So far, we have learned 3 types of accessibility modifiers:

      Modifier       Accessed       Accssed         Access 
      on members     from the       from the        from a
      in a class     same class     same package    diff. package
     ============================================================
      public            OK             OK              OK           
      default           OK             OK              No           
      private           OK	           No              No           


  • Java has one more access modifier:

    • protected

Level of trust of code in Java classes

  • To understand the accessibility modifiers you need to understand that:

    • A large programming project is coded by many different programmers/teams

    • There are degree of trust of program code written by different programmers/teams

  • The degree of trust of classes in a programming project:

      1. Code inside a class is usually written by the same programmer (or at most a few programmers) --- highest level of trust

      2. Code inside a package is usually written by same team of the programmers --- 2nd highest level of trust

      3. Code inside a different package can be written by any programmer...
        --- Lowest level of trust

Level of trust of code in Java classes

  • The degree of trust of classes in a programming project:

      1. Code inside a class is usually written by the same programmer (or at most a few programmers) --- highest level of trust

      2. Code inside a package is usually written by same team of the programmers --- 2nd highest level of trust

      3. Code inside a different package can be written by any programmer...
        --- Lowest level of trust

  • Accesibility assigned to the degree of trust:

      Trust:       Highest     2nd Highest                     Lowest
    
      Modifier     Accessed    Accssed       Access            Access 
      on members   from the    from the      from subclass in  from a
      in a class   same class  same package	 diff. package     diff. package
     =================================================================== 
      public           OK         OK             OK               OK
      protected
      default          OK         OK	     No		      No
      private          OK	      No	     No               No  

The protected accessibility modifier

  • We will now study the protected accessibility modifier

  • Syntax to define a member with protected accessibility:

        protected memberDefinition; 

    Meaning:

    • The protected modifier will allow any subclass (written by any programmer) to access this data fields or methods

  • Where can you access a member with protected accessibilitity:

    1. From inside a method in the same class (closest association)

    2. From inside a method in the same package (2nd closest association)

    3. From inside a method in a subclass defined outside the package (new!)

    4. But not from inside a method in a unrelated class defined outside the package

The protected accessibility modifier

  • Accesibility modifier diagram that we have learned so far:

      Modifier     Accessed    Accssed       Access            Access 
      on members   from the    from the      from subclass in  from a
      in a class   same class  same package	 diff. package     diff. package
     =================================================================== 
      public           OK         OK             OK               OK
      protected
      default          OK         OK	     No		      No
      private          OK	      No	     No               No  

  • Accesibility modifier diagram with the protected accessibility modifier:

      Modifier     Accessed    Accssed       Access            Access 
      on members   from the    from the      from subclass in  from a
      in a class   same class  same package	 diff. package     diff. package
     =================================================================== 
      public           OK         OK             OK               OK
      protected        OK         OK             OK               No     
      default          OK         OK	     No		      No
      private          OK	      No	     No               No 

Example that show protected members can be accessed from a method in the same package

This example shows that protected access variables can be accessed (= used) by methods in the same package:

package Geometrics;

public class Demo
{
    public static void main(String[] args)
    {
        Circle c = new Circle("red", 2);
        System.out.println(c.radius);
    }
}





 // We can access the protected access
 // variable radius in Circle
 // because they are in the same package
    








 
package Geometrics;

public class Circle extends GeometricObject
{
    protected double radius;

    public Circle1(String col, double r)
    {
        super(col);
        radius = r;
    }

    public double getRadius()         
    { 
        return radius;
    }

    public double getArea()
    {
        return 3.14159*radius*radius;
    }

    public String toString()
    {
        return "Color = " + getColor() + ":"
                + "radius = " + radius;
    }
} 

DEMO: 04-inheritance/22-protected/Geometrics/Demo.java + Circle.java

Example that show protected members can be accessed from a subclass in a different package

This example shows that protected access variables can be accessed (= used) by methods in a subclass:

import Geometrics.Circle; // Must import!

public class NewCircle extends Circle
{
    NewCircle()
    {
        super("red", 1);
    }
    
    public void accessInheritedProtected()
    {
        System.out.println(radius);
    }
}

 // We can access the protected access
 // variable radius in Circle
 // because NewCircle is a subclass
 

   
  





 
package Geometrics;

public class Circle extends GeometricObject1
{
    protected double radius;

    public Circle1(String col, double r)
    {
        super(col);
        radius = r;
    }

    public double getRadius()         
    { 
        return radius;
    }

    public double getArea()
    {
        return 3.14159*radius*radius;
    }

    public String toString()
    {
        return "Color = " + getColor() + ":"
                + "radius = " + radius;
    }
} 

DEMO: 04-inheritance/22-protected/NewCircle.java + Demo.java

Summary

  • When you define a class, determine the accessibility of the members in the class as follows:

      • If a variable or method should only be used inside this class:

           private
        

      • If a variable or method will be used by other members in your project group:

           default
        

      • If direct access of a variable or method is necessary for a subclass:

           protected
        

      • In all other cases:

           public