Review:    accessibility modifiers

  • 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         open
      default          OK         OK            No
      private          OK	      No            No         limited

  • The default accessibility modifier was discussed in Chapter 9.8 in the text book and my material is here:


  • Java has one more access modifier that:

    • Allow a subclass method to access specific members in the superclass

    • Disallow a non-subclass method from accessing these members

Level of trust of code in Java classes

  • To understand the accessibility modifiers you need to understand:

    • The degree of trust (= closeness) of program code written by different people

  • The degree of closeness (= trust) of classes in a Java program:

      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 the same team of programmer --- 2nd highest level of trust

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


  • We will now study the protected accessibility modifier

  • The protected modifier will allow subclasses inside a different package (written by anyone) to access data fields or methods in the superclass
    --- Level of trust = ?

The protected accessibility modifier

  • Syntax to define a member with protected accessibility:

        protected memberDefinition; 

  • 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

package Geometrics;

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


   /* --- No compile/run error --- */

   Current directory: 
     04-inheritance/22-protected/Geometrics

   Compile:  javac -cp .. Demo.java

   Run with: java -cp .. Geometrics/Demo







 
package Geometrics;

public class Circle1 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/Geometrics

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

import Geometrics.*;

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

   /* --- No compile/run error --- */


   Current directory:
       demo/04-inheritance/22-protected

   Compile:   javac Demo.java
   Run with:  java  Demo




 
package Geometrics;

public class Circle1 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

  • A class can be use in 2 different ways:

      1. for creating instances of the class (with new)

      2. for defining subclasses by extending the class

  • Assign accessibility to members in a class as follows:

      • private if a member is not to be used from outside the class.

      • "default" if a member is not to be used from outside the package.

      • public if a member to be used by any user

      • protected if a member is intended for the extenders of the class but not for all users