So far,
we have learned3 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 defaultaccessibility modifier
was
discussed in
Chapter 9.8 in the
text book and
my material is
here:
Java has
one moreaccess 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 codewritten by
different people
The degree of closeness (= trust) of
classes in a
Java program:
Code inside
a
class is
usually written by
the
same programmer
(or at most a fewprogrammers) ---
highest level of trust
Code inside
a
package is
usually written by
the same team of programmer
---
2nd highest level of trust
Code inside
a
different package
can be written by
anyone...
---
Lowest level of trust
We will nowstudy the
protectedaccessibility modifier
The
protected modifier
will
allowsubclasses
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
protectedaccessibility:
protected memberDefinition;
Where can you
access a
member with
protected accessibilitity:
From inside a method in the
same class
(closest association)
From inside a method in the
same package
(2nd closest association)
From inside a
method in a
subclass
defined
outside
the package
(new!)
Butnot from
inside a method in a
unrelated class
defined outside
the package
The
protected
accessibility modifier
Accesibility modifierdiagram that we
have learnedso 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 modifierdiagramwith the
protectedaccessibility 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;
}
}