Java Reference
In-Depth Information
Let's look at an example that demonstrates how the access specifier of a
member controls access to the members of an object. The InventoryItem class
defined in the following code is in the products package. Review the class and
determine its fields, methods, and constructors, and their corresponding
access.
package products;
public class InventoryItem
{
private long partNumber;
public String description;
public InventoryItem(long n, String d)
{
partNumber = n;
description = d;
}
protected InventoryItem()
{
partNumber = 0;
description = “N/A”;
}
long getPartNumber()
{
return partNumber;
}
}
The DVDPlayer class in the following extends InventoryItem, but is in a dif-
ferent package. DVDPlayer can therefore only access the public and protected
members of InventoryItem, and does not have access to the private or default
members.
package electronics;
import products.InventoryItem;
public class DVDPlayer extends InventoryItem
{
public String make, model;
private double retailPrice;
public DVDPlayer(String make, String model, long partNumber)
{
super(partNumber, “DVD player”);
this.make = make;
this.model = model;
}
public DVDPlayer(String make, String model)
{
super();
Search WWH ::




Custom Search