Java Reference
In-Depth Information
If a class member is declared
protected
, it is accessible in the package in which it is declared. A
protected
class
member is always accessible inside the body of a subclass whether the subclass is in the same package as the class
or in a different package. A
protected
class member is inherited by a subclass. The
protected
access modifier is
used with a class member when you want subclasses to access and inherit the class member. Note that a
protected
class member can be accessed through the package in which it is declared and inside subclasses. If you want to
provide access to a class member only from inside its package, you should use a package-level access modifier, not a
protected
access modifier.
If a class member is declared package-level, it is accessible only inside the package in which the class is declared.
A package-level class member is inherited only if the superclass and subclass are in the same package. If the
superclass and the subclass are in different packages, the subclass does not inherit package-level members from
its superclass.
■
the access modifiers build on each other where you start with no access to the outside world (private), and add
to it, first package (default), then subclasses (protected), and then the world (public).
Tip
Let's look at your example of inheritance in Listing 16-1 and Listing 16-2. The
Employee
class has three members:
a
name
field, a
getName()
method, and a
setName()
method. The
name
field has been declared
private
and hence it
is not accessible inside the
Manager
class because it is not inherited. The
getName()
and
setName()
methods have
been declared
public
and they are accessible from anywhere including the
Manager
class. They are inherited from the
Employee class by the
Manager
class, though since they are public, the fact they are inherited doesn't matter.
Upcasting and Downcasting
An “is-a” relationship in the real world translates into inheritance class hierarchy in software. A class is a type in
Java. When you express the “is-a” relationship using inheritance, you create a subclass, which is a more specific type
of the superclass. For example, a
Manager
is a specific type of
Employee
. An
Employee
is a specific type of
Object
.
As you move up in the inheritance hierarchy, you move from a specific type to a more general type. How does
inheritance affect the client code? In this context, the client code is any code that uses the classes in a class hierarchy.
Inheritance guarantees that whatever behavior is present in a class will also be present in its subclass. A method in
a class represents a behavior of the objects of that class. This means that whatever behavior a client code expects to
be present in a class will also be present in the class's subclass. This leads to the conclusion that if client code works
with a class, it will also work with the class's subclass, because a subclass guarantees at least the same behaviors as its
superclass. For example, the
Manager
class provides at least the same behaviors as provided by its superclass
Employee
.
Consider the following snippet of code:
Employee emp;
emp = new Employee();
emp.setName("Richard Castillo");
String name = emp.getName();
This snippet of code compiles without any errors. When the compiler comes across
emp.setName("Richard
Castillo")
and
emp.getName()
calls, it checks the declared type of the
emp
variable. It finds that the declared type
of the
emp
variable is
Employee
. It makes sure that the
Employee
class has
setName()
and
getName()
methods that
conform to the call being made. It finds that the
Employee
class does have a
setName()
method that takes a
String
as
a parameter. It finds that the
Employee
class does have a
getName()
method that takes no parameters and returns a
String
. After verifying these two facts, the compiler is fine with the
emp.setName()
and
emp.getName()
method calls.