Java Reference
In-Depth Information
between package access and protected accesses of a member. If a member of a class has a
package access, that member can be directly accessed in any class contained in that package,
but not in any class that is not contained in that package even if a subclass is derived from
the class containing that member and the subclass is not contained in that package. On the
other hand if a member of a class is
protected
, that member can be directly accessed in any
subclass even if the subclass is contained in a different package.
Consider the following class definition:
public class
Rectangle
{
double
length;
double
width;
public
Rectangle()
{
length = 0;
width = 0;
}
double
area()
{
return
length * width;
}
.
.
.
}
In this class definition, the data members
length
and
width
and the method
area
have
package access.
1
0
In Chapter 8, we defined the
class
Clock
and later included the method
toString
to
return the time as a string. When we included the method
toString
, we noted that every
Java class (built-in or user-defined) is automatically provided the method
toString
.Ifa
user-defined class does not provide its own definition of the method
toString
,thenthe
default definition of the method
toString
is invoked. The methods
print
and
println
use the method
toString
to determine what to print. As shown in Chapter 8, the default
definition of the method
toString
returns the class name followed by the hash code of the
object. You might ask, where is the method
toString
defined?
The method
toString
comes from the Java
class
Object
, and it is a
public
member
of this class. In Java, if you define a class and do not use the reserved word
extends
to
derive it from an existing class, then the class you define is automatically considered to be
derived from the
class
Object
. Therefore, the
class
Object
directly or indirectly
becomes the superclass of every class in Java. From this, it follows that the definition of
the
class
Clock
(previously given in Chapter 8):
Search WWH ::
Custom Search