Review:
accessibility modifiers
"Closeness" of association
of code in Java classes
- To understand the
accessibility modifiers
you need to understand
the degree of
closeness (= trust) of
programs
written 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 few
programmers)
- Code inside
a
package is
usually written by
the
same team of programmer
(or at most a few
programmers)
- Code inside
a
different package
can be written by
anyone...
|
- We will now
learn about the
protected
accessibility modifier
- The
protected modifier
will
allow
subclasses
(defined by anyone)
to access
data fields or methods
defined in the
superclass,
|
The
protected
accessibility modifier
- Syntax to
define a
member with
protected
accessibility:
protected memberDefinition;
|
- Where can you
access a
member with
protected accibilitity:
- 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!)
|
|
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)
{
Circle1 c = new Circle1("red", 2);
System.out.println(c.radius);
}
}
/* --- No compile error --- */
|
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;
}
}
|
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 error --- */
|
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;
}
}
|
Summary
- A class can be use
in 2 different ways:
- for
creating instances
of the class
(with
new)
- 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
|
|
❮
❯