Class members and their
"visibility"
- Review:
- Class member =
a variable or
a method defined
in a class
|
- 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
o.m1(); // Invokes a public method
o.m2(); // Invokes a private method
}
}
|
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 will
reduce
accidental update
of variable(s)
by a
method from
another class !!!
|
Java packages
- Some problems are
so complex that
they cannot be solved
by methods inside
one single class
- We may need to write
multiple classes to
solve such
complex problems
-
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
|
- 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
How to create a
class
inside a
package p1 in BlueJ
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
The
default access modifier
The
default access modifier
Demo:
use BlueJ
package p1;
public class C1
{
public int x1;
int x2;
public void m1() {}
void m2() {}
}
package p1;
public class C2
{
void aMethod()
{
C1 o = new C1();
o.x1 = 1;
o.x2 = 99; // No compile error
o.m1();
o.m2(); // No compile error
}
}
|
The
default access modifier
The
default access modifier
Demo:
use BlueJ
package p1;
public class C1
{
public int x1;
int x2;
public void m1() {}
void m2() {}
}
import p1.C1;
public class myClass
{
void aMethod()
{
C1 o = new C1();
o.x1 = 1;
o.x2 = 99; // Compile error
o.m1();
o.m2(); // Compile error
}
}
|
Summary
Members with the
default access modifier can
be accessed from
methods in
classes inside the
same package:
❮
❯